Search code examples
gitsparse-checkout

Does sparse-checkout act on tracked file?


My local and remote server both are centos 7.
I tracked README.md in local repo with git version 1.8.3.1.When I revised README.md and push to remote bare repo, I don't want README.md to be checked out.
In remote repo,git version is 2.16.6,I set post-receive as below:

while read oldrev newrev ref
do
     if [[ $ref = refs/heads/"$BRANCH" ]];
        then
        git --work-tree="$DEPLOY_DIR"  --git-dir="$GIT_DIR" checkout -f
     fi
done

In remote repo,I set sparse-checkout as below:

/*
!/README.md

I found README.md still there,and not modified.

Then,I revised sparse-checkout as below:

!/README.md
/*

I got remote: error: Entry 'README.md' not uptodate. Cannot merge.

which sparse-checkout is correct?Where is the problem?


Solution

  • It's time to play spot-the-differences. Here's my test setup, it shows the sparse checkout working properly when I doit. This is a straight c&p out of my terminal buffers, copy the function definition and say doit.

    $ doit () { 
    set -x
    git init testlocal; cd $_
    git commit --allow-empty -m-
    git clone --bare . ../testremote; cd $_
    git config core.sparsecheckout true
    mkdir -p hooks info worktree
    printf %s\\n /\* \!/README.md >info/sparse-checkout
    tee hooks/post-receive <<'EOD'; chmod +x $_
    while read oldrev newrev ref; do
            if [[ $ref = refs/heads/master ]]; then
                    git --work-tree="$GIT_DIR/worktree" checkout -f
            fi
    done
    EOD
    cd -
    touch notREADME.md README.md
    git add .; git commit -m-
    git push ../testremote master
    ls ../testremote/worktree
    set +x
    }
    $ doit
    + git init testlocal
    Initialized empty Git repository in /home/jthill/src/snips/testlocal/.git/
    + cd testlocal
    /home/jthill/src/snips/testlocal
    + git commit --allow-empty -m-
    [master (root-commit) 6ed44ec] -
    + git clone --bare . ../testremote
    Cloning into bare repository '../testremote'...
    done.
    + cd ../testremote
    + git config core.sparsecheckout true
    + mkdir -p hooks info worktree
    + printf '%s\n' '/*' '!/README.md'
    + tee hooks/post-receive
    while read oldrev newrev ref
    do
         if [[ $ref = refs/heads/master ]];
            then
            git --work-tree="$GIT_DIR/worktree" checkout -f
         fi
    done
    + chmod +x hooks/post-receive
    + cd -
    /home/jthill/src/snips/testlocal
    + touch notREADME.md README.md
    + git add .
    + git commit -m-
    [master 64f5358] -
     2 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 README.md
     create mode 100644 notREADME.md
    + git push ../testremote master
    Enumerating objects: 4, done.
    Counting objects: 100% (4/4), done.
    Delta compression using up to 4 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 240 bytes | 240.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    To ../testremote
       6ed44ec..64f5358  master -> master
    + ls --color=auto ../testremote/worktree
    notREADME.md
    + set +x