问题
In my other question, Issue in automatic executing post-commit in git hook to sync local and remote directory,
I could use git hook to invoke post-receive
so that the working directory is the same as git bare repository.
unset GIT_INDEX_FILE
git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f
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.
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?
+
+????
I tried add/commit/push from the server's working directory to get this error.
ubuntu@ip-172-31-63-19 dumb> git push
To /home/ubuntu/git/dumb.git/
! [rejected] master -> 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.
What might be wrong? How to sync the stage(index) and working directory?
Reference: https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks
EDIT
From What's the difference between "git reset" and "git checkout"?, I could run git reset --hard
in the working directory to make the sync, but the changes pushed are overwritten. Then, with git push
, I could get the updated version, but I needed a merge.
This is a git log --pretty=oneline
from server's working directory.
86461a03f7d46bbc90d1ef47ae3a21774848407f Merge branch 'master' of /home/ubuntu/git/dumb
049111edb0e612ec3bf364ea01423cdea8575560 1234
f506be37c3cd6d24ec727ebfb551c9d5dbd780b5 ?*
ca5a0f5efb90788c3e1669d192d3fb333fdbcd72 ?
This is from local directory
049111edb0e612ec3bf364ea01423cdea8575560 1234
f506be37c3cd6d24ec727ebfb551c9d5dbd780b5 ?*
ca5a0f5efb90788c3e1669d192d3fb333fdbcd72 ?
回答1:
The issue is that even though the working directory contains the server's newest update, the index and history in the local git directory is not updated.
I don't think this is the best solution, but in the server's working directory, I could sync to the latest update.
- git stash
- git pull
- git stash drop
It stashes the changes in the working directory and index/history in order to prevent the collision, then pull to sync and throw away the stash. For now, I can automatically sync from my local directory to server's working directory, and I can update the change from time to time.
Though I expect a full solution, not this temporary solution.
来源:https://stackoverflow.com/questions/33812206/how-to-sync-the-stageindex-and-working-directory-after-git-hook-post-receive