I switched to master after developing 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 because I have checkout an intermediate commit on master.
How to make master stay on head?
This question is related to
git
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
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
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
.
In your case, to fast-forward, run:
$ git merge --ff-only origin/master
This uses the --ff-only
option of git merge
, as the question specifically asks for "fast-forward".
Here is an excerpt from git-merge(1)
that shows more fast-forward options:
--ff, --no-ff, --ff-only
Specifies how a merge is handled when the merged-in history is already a descendant of the current history. --ff is the default unless merging an annotated
(and possibly signed) tag that is not stored in its natural place in the refs/tags/ hierarchy, in which case --no-ff is assumed.
With --ff, when possible resolve the merge as a fast-forward (only update the branch pointer to match the merged branch; do not create a merge commit). When
not possible (when the merged-in history is not a descendant of the current history), create a merge commit.
With --no-ff, create a merge commit in all cases, even when the merge could instead be resolved as a fast-forward.
With --ff-only, resolve the merge as a fast-forward when possible. When not possible, refuse to merge and exit with a non-zero status.
I fast-forward often enough that it warranted an alias:
$ git config --global alias.ff 'merge --ff-only @{upstream}'
Now I can run this to fast-forward:
$ git ff
To anyone that wants to Fast Forward they are not on to another remote branch (including itself) without checking out that branch, you can do:
git fetch origin master:other
This basically fast forwards the index of other
to origin/master
if you are not on other
branch. You can fast forward multiple branches this way.
If you working on another branch for some time, and wanted to update stale branches from remote to their respective head:
git fetch origin master:master other:other etc:etc
Move your branch 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 ;)
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
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.
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
In cases where you're somewhere behind on a branch,
it is safer to rebase any local changes you may have:
git pull --rebase
This help prevent unnecessary merge-commits.
Source: Stackoverflow.com