How to fast-forward a branch to head?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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).

Solution 3 - 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

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

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

Solution 7 - Git

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

Solution 8 - Git

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

Solution 9 - Git

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.

Solution 10 - Git

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.

(this is the same as doing git fetch && git rebase)

Solution 11 - Git

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 ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpengguang001View Question on Stackoverflow
Solution 1 - Gitrob mayoffView Answer on Stackoverflow
Solution 2 - GitGreg HewgillView Answer on Stackoverflow
Solution 3 - GitvoidvectorView Answer on Stackoverflow
Solution 4 - GitKubaView Answer on Stackoverflow
Solution 5 - GitSunny PatelView Answer on Stackoverflow
Solution 6 - GitjontroView Answer on Stackoverflow
Solution 7 - GitaudeView Answer on Stackoverflow
Solution 8 - GitHasan Junaid HashmiView Answer on Stackoverflow
Solution 9 - GitbobahView Answer on Stackoverflow
Solution 10 - GitGonenView Answer on Stackoverflow
Solution 11 - GitDo AsyncView Answer on Stackoverflow