问题
I have a local repository that I've cloned from my remote repository (all on one machine). I wanted to make sure that my remote repository picked the changes from my local repository so I did a git push origin
.
I changed my working directory to my remote repository; the change had propagated to the log file (i.e. doing git log
showed the change) but my actual working directory didn't show the change. I did a git checkout HEAD
but the CWD still didn't change. It wasn't until I did a git checkout --force HEAD
that the CWD synced up.
I suspect this is happening because the remote repository isn't a bare repository. So two questions:
- Is there a way that I can make the remote repository automatically sync (i.e. discard local changes) on a
git push
? - Why do I need to to use
--force
to get it to sync up? What should be the process of syncing it up?
回答1:
If your push (to a non-bare) repo succeeded, it is because:
- either you pushed a branch which wasn't checked out on your remote repo (the non-bare remote repo)
- or
receive.denyCurrentBranch
was set tofalse
orignore
in the git config
In the latter case, any current local modification wouldn't be erased by that push.
Only a git checkout --force
would reset the working directory to the actual index content referenced by HEAD
.
The usual way to ensure synchronization between two non-bare repo is by adding an intermediate bare repo (to which you push) with a post-receive hook which will go the actual remote repo (the non-bare one) and pull from the updated bare repo.
The usual example: Using Git to manage a web site, which I use in:
- "Push to remote server and Github"
- "How to reset staged changes on the remote after a push?"
- "How push directly to remote server just using push?"
When you are pulling, make sure to unset GIT_DIR
: see "Calling 'git pull' from a git post-update hook"
来源:https://stackoverflow.com/questions/11592452/why-do-i-need-to-force-git-to-sync-my-remote-repository