可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I switch to master after develop on a branch for a long time. The log shows:
Your branch is behind 'origin/master' by 167 commits, and can be fast-forwarded.
I tried:
git checkout HEAD
It has no effect. This is cause because that I have checkout out an intermediate commit on master.
How to make master stay on head?
回答1:
Doing:
git checkout master git pull origin
will fetch and merge the origin/master
branch (you may just say git pull
as origin is the default).
回答2:
Try git merge origin/master
. If you want to be sure that it only does a fast-forward, you can say git merge --ff-only origin/master
.
回答3:
In your situation, git rebase
would also do the trick. Since you have no changes that master doesn't have, git will just fast-forward. If you are working with a rebase workflow, that might be more advisable, as you wouldn't end up with a merge commit if you mess up.
username@workstation:~/work$ git status # On branch master # Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded. # (use "git pull" to update your local branch) # nothing to commit, working directory clean username@workstation:~/work$ git rebase First, rewinding head to replay your work on top of it... Fast-forwarded master to refs/remotes/origin/master. # On branch master nothing to commit, working directory clean
回答4:
git checkout master git pull
should do the job.
You will get the "Your branch is behind" message every time when you work on a branch different than master, someone does changes to master and you git pull.
(branch) $ //hack hack hack, while someone push the changes to origin/master (branch) $ git pull
now the origin/master reference is pulled, but your master is not merged with it
(branch) $ git checkout master (master) $
now master is behind origin/master and can be fast forwarded
this will pull and merge (so merge also newer commits to origin/master) (master) $ git pull this will just merge what you have already pulled (master) $ git merge origin/master
now your master and origin/master are in sync
回答5:
If you are standing on a different branch and want to checkout the newest version of master you can also do
git checkout -B master origin/master
回答6:
No complexities required just stand at your branch and do a git pull it worked for me
Or, as a second try git pull origin master only in case if you are unlucky with the first command
回答7:
Move your brunch pointer to the HEAD:
git branch -f master
Your branch master
already exists, so git will not allow you to overwrite it, unless you use... -f
(this argument stands for --force
)
Or you can use rebase:
git rebase HEAD master
Do it on your own risk ;)
回答8:
To rebase the current local tracker branch moving local changes on top of the latest remote state:
$ git fetch && git rebase
More generally, to fast-forward and drop the local changes (hard reset)*:
$ git fetch && git checkout ${the_branch_name} && git reset --hard origin/${the_branch_name}
to fast-forward and keep the local changes (rebase):
$ git fetch && git checkout ${the_branch_name} && git rebase origin/${the_branch_name}
* - to undo the change caused by unintentional hard reset first do git reflog
, that displays the state of the HEAD in reverse order, find the hash the HEAD was pointing to before the reset operation (usually obvious) and hard reset the branch to that hash.