title: How to sync the stage(index) and working directory after git hook post-receive?
body: <p>In my other question, <a href="https://stackoverflow.com/questions/33810949/issue-in-automatic-executing-post-commit-in-git-hook-to-sync-local-and-remote-di">Issue in automatic executing post-commit in git hook to sync local and remote directory</a>, 
  I could use git hook to invoke <code>post-receive</code> so that the working directory is the same as git bare repository. </p>
  
  <pre><code>unset GIT_INDEX_FILE
  git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f
  </code></pre>
  
  <p>After the push the working directory is revised, but the index is not updated. 
  For example, in my local machine, I add a line "???" in hello.txt. I added/commited/push the change to automatically update the hello.txt in server's working git directory. </p>
  
  <pre><code>diff --git a/hello.txt b/hello.txt
  index 3ab3c7a..b63ad40 100644
  --- a/hello.txt
  +++ b/hello.txt
  @@ -4,3 +4,5 @@ Again, helloooo, world?
   Hello, world? Automatically?
  
   How about this?
  +
  +????
  </code></pre>
  
  <p>I tried add/commit/push from the server's working directory to get this error.</p>
  
  <pre><code>ubuntu@ip-172-31-63-19 dumb&gt; git push
  To /home/ubuntu/git/dumb.git/
   ! [rejected]        master -&gt; master (fetch first)
  error: failed to push some refs to '/home/ubuntu/git/dumb.git/'
  hint: Updates were rejected because the remote contains work that you do
  hint: not have locally. This is usually caused by another repository pushing
  hint: to the same ref. You may want to first integrate the remote changes
  hint: (e.g., 'git pull ...') before pushing again.
  hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  </code></pre>
  
  <p>What might be wrong? How to sync the stage(index) and working directory?</p>
  
  <p>Reference: <a href="https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks" rel="nofollow noreferrer">https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks</a></p>
  
  <h2>EDIT</h2>
  
  <p>From <a href="https://stackoverflow.com/questions/3639342/whats-the-difference-between-git-reset-and-git-checkout">What&#39;s the difference between &quot;git reset&quot; and &quot;git checkout&quot;?</a>, I could run <code>git reset --hard</code> in the working directory to make the sync, but the changes pushed are overwritten. Then, with <code>git push</code>, I could get the updated version, but I needed a merge. </p>
  
  <p>This is a <code>git log --pretty=oneline</code> from server's working directory. </p>
  
  <pre><code>86461a03f7d46bbc90d1ef47ae3a21774848407f Merge branch 'master' of /home/ubuntu/git/dumb
  049111edb0e612ec3bf364ea01423cdea8575560 1234
  f506be37c3cd6d24ec727ebfb551c9d5dbd780b5 ?*
  ca5a0f5efb90788c3e1669d192d3fb333fdbcd72 ?
  </code></pre>
  
  <p>This is from local directory</p>
  
  <pre><code>049111edb0e612ec3bf364ea01423cdea8575560 1234
  f506be37c3cd6d24ec727ebfb551c9d5dbd780b5 ?*
  ca5a0f5efb90788c3e1669d192d3fb333fdbcd72 ?
  </code></pre>