Error "Fatal: Not possible to fast-forward, aborting"
Asked Answered
A

21

712

Why is Git not allowing me to fast forward merge anymore?

If I try to force it using --ff-only, I get the message

fatal: Not possible to fast-forward, aborting.

I realize that there are huge advantages to merge --no-ff, but I'm just puzzled why I can't --ff-only now?

Aposematic answered 28/10, 2012 at 3:40 Comment(0)
M
245

Your branch is no longer directly based off of the branch you're trying to merge it into - e.g. another commit was added to the destination branch that isn't in your branch. Thus, you can't fast-forward into it (because fast-forward requires your branch to completely contain the destination branch).

You can rebase your branch on top of the destination branch (git rebase <destination branch>) to rework the commits such that they will fast forward into it, or you can do a regular merge.

Milkman answered 28/10, 2012 at 3:57 Comment(6)
How is this possible? This is not correct. It must be some setting that has changed.Edi
@Edi this message comes from your pull config being set to ff from the command git config pull.ff onlyIgraine
"fast-forward requires your branch to completely contain the destination branch" seems backward. Should be "fast-forward requires your branch to be completely contained inside the destination branch"Sholapur
@Sholapur no; you've reversed what "your" means here.Milkman
just run this: git pull origin <branch> --rebaseIntervene
@Intervene ... again fatal: Not possible to fast-forward, aborting.Nutpick
P
1104

Disclaimer: these commands will bring changes from the remote branch into yours.

git pull --rebase. Unlike the other solution, you don't need to know the name of your destination branch.

If your upstream branch is not set, try git pull origin <branch> --rebase (credit to @Rick in the comments)

To set this option globally, use git config --global pull.rebase true (credit to @Artur Mustafin below)

Prunella answered 17/4, 2017 at 22:47 Comment(5)
This worked great for me except I didn't have an upstream set on my branch so I had to use git pull origin <branch> --rebase and it seemed to do the trick. Thanks!Rounded
If you get merge conflicts in the middle of this, fix your merge files and afterwards run git rebase --continue.Verbena
i did git push to remote before pulling and was getting the error and this fix worked for me. thanks!Pneumatograph
My commit is conflicted with my fellow and this worksJumpoff
I noticed recently, that if the git rebase takes a while and someone else merges their branch, when you do your PR, their files are included. Someone explained it to me as "pre-commit hook included those files"Haploid
W
554

Use the option --no-ff to turn off fast-forwarding for one pull:

git pull --no-ff

https://git-scm.com/docs/git-pull#Documentation/git-pull.txt---no-ff

Wendelina answered 17/6, 2021 at 13:52 Comment(3)
This is the correct answer, in the event that you are specifically trying to avoid doing a rebase.Peroxidize
Is it possible to make this default? So--no-ff is not required?Astonishment
@AlvinCHENG Please have a look #5519507Wendelina
M
245

Your branch is no longer directly based off of the branch you're trying to merge it into - e.g. another commit was added to the destination branch that isn't in your branch. Thus, you can't fast-forward into it (because fast-forward requires your branch to completely contain the destination branch).

You can rebase your branch on top of the destination branch (git rebase <destination branch>) to rework the commits such that they will fast forward into it, or you can do a regular merge.

Milkman answered 28/10, 2012 at 3:57 Comment(6)
How is this possible? This is not correct. It must be some setting that has changed.Edi
@Edi this message comes from your pull config being set to ff from the command git config pull.ff onlyIgraine
"fast-forward requires your branch to completely contain the destination branch" seems backward. Should be "fast-forward requires your branch to be completely contained inside the destination branch"Sholapur
@Sholapur no; you've reversed what "your" means here.Milkman
just run this: git pull origin <branch> --rebaseIntervene
@Intervene ... again fatal: Not possible to fast-forward, aborting.Nutpick
U
123

If

git pull

does not do the trick and if you want to merge both the current changes and the changes that'd come from the pull of the branch from origin then do this:

git merge origin/BRANCH_NAME

After that, resolve the merge conflicts if any and be done for the day.

Uncrowned answered 2/3, 2021 at 13:57 Comment(4)
doesn't this affect the brach you're pulling from?Pleasantry
@Pleasantry no, it applies incoming changes to your local branchDispensable
Worked!!!! So in my case I was pulling from parent branch to child branch, child branch had few different changes and parent one is just keep going.Jabin
git merge origin/BRANCH_NAME this worked for me. Doing rebase is not a good solution which is suggested in other answersSubbase
R
89

Try this. It will work nicely.

git pull origin --rebase 

After you get the non-fast-forward error, just do below :

git pull --rebase origin <name-of-the-remote-branch>

This will fetch the remote changes to your local branch. On top of that, it will apply your local commits.

Then

git push origin <name-of-the-remote-branch>

This will apply your local changes in the local copy of the remote branch to the actual remote branch repo in git

** Recommended

Also -you can try this command

git pull --no-ff

Response in the terminal-

  1. Please enter a commit message to explain why this merge is necessary.
  2. especially if it merges an updated upstream into a topic branch.
  3. Lines starting with '#' will be ignored, and an empty message aborts the commit.

After that normally git add, commit and push

Rhearheba answered 21/11, 2022 at 10:1 Comment(3)
Welcome to SO! Please refer to How do I write a good answer guidline to improve your contribution.Chrysanthemum
ty for the plain-spoken answer unlike all the gitese.Pl
Did not work for me. Everytime I tried to pull from git I would get the error" fatal: Not possible to fast-forward, aborting."Simplify
P
57

This is because you have enabled the fast-forward only option. The thing here is your pull from the branch will create a merge commit in your local git and the fast-forward only option doesn't allow creating a merge commit at the time of pull.

In the case of a big team, You will end up rebasing and resolving conflicts lots of the time and for each and every commit coming from the pull.

I suggest you remove ff = only line from git local config file.

$ cd to-my-project-root-dir

$ nano .git/config

[pull]
        ff = only // remove this line
        rebase = false

Plimsoll answered 13/12, 2020 at 20:48 Comment(2)
It might only happen if ff = only is enabled, but that is not the cause. If the error happens its a sign that something non-standard has happened to master see https://mcmap.net/q/64589/-not-possible-to-fast-forward-even-with-no-changes for a good way to determine what happened. For example I had this error trying to pull from master, and what I'd actually done was commit something locally on master which didn't exist in the remote. On a team we should never be committing to master or pushing to master; if I had disabled ff then it would not have shown me my mistake.Tyrannize
After all researches, your answer worked pretty well. In the file .git/config, remove ff = only, and add rebase = false.Paolo
T
37

For me this worked. Change branch_name to the actual name of the branch:

git pull origin branch_name --no-ff
Tical answered 1/11, 2022 at 16:44 Comment(2)
worked but have to fix the conflicts manually after it.Failsafe
Worked for me. In my case, the commits had no conflicting files from the remote repo. So worked like a charm :)Beneficiary
S
33

If you get this when doing a git pull origin master on your local branch, open .gitconfig in Notepad (usually hidden in C:\Users\Myname) and add these two lines

[pull]
    ff = no

Save the config and try git pull origin master again

Shadwell answered 7/7, 2021 at 18:49 Comment(2)
git pull origin master --ff/--no-ff/--ff-only are the respective one time options.Sidestep
To solve it on MacOs, you can edit the config file on .git/config.Paolo
F
21

This works for me

git pull --no-ff

Then if you face this thing,

Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

then follow these steps:

  1. press i
  2. pressEsc
  3. press :wq
Fungistat answered 3/10, 2022 at 4:10 Comment(0)
B
18

You can try this in the terminal, which actually edits your <.gitconfig> file and set ff = no

git config pull.ff no
Bribery answered 10/6, 2022 at 5:39 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Scruggs
Thank you, this removes the hassle of using --no-ff flag every time I'm doing a pullSikang
P
17

It works for me. You can use:

git pull --no-ff

Peltier answered 5/7, 2022 at 9:33 Comment(1)
Cannot save the current worktree state fatal: stash failedCashier
C
16

git pull origin --no-ff fixed the problem for me.

Carrero answered 6/11, 2023 at 11:41 Comment(1)
Worked for me as wellOutplay
L
12

Use:

git pull origin main --rebase
git pull --rebase

The above two commands worked for me. But the only problem is I see the commits from the main branch in the current branch too...

Lefthand answered 20/9, 2022 at 9:53 Comment(2)
Also when I saw this issue second time, I tried this coz I didn't like rebase because of obvious reasons and it worked. git merge main Or you can delete local branch git branch -d <BRANCH_NAME> and then git checkout <BRANCH_NAME> and then git merge mainLefthand
Incredible how always the least upvoted answer solves the problem most of the time. Thank youSelfknowledge
B
5

When will I see this message?

When you run git merge --ff-only <some/branch> or git pull --ff-only <remote> <some/branch>, and the branch or commit you are trying to merge is not based off your current branch—its history has somehow forked from yours.

git pull can have defaults set in the configuration, so you can also see this if you ran a plain git pull origin <some/branch>, and your config has pull.ff = only. To check your config: run git config pull.ff or git config --show-origin pull.ff (git merge has a similar merge.ff option)

How can I see what doesn't work?

To view the history of both your branch and the target branch, you can use:

git log --graph --oneline HEAD <some/branch>

If you didn't explicitly type a branch name (e.g: git merge --ff-only or git pull --ff-only), git defaults to "the upstream branch of your active branch" -- it often is origin/mybranch. A way to reference that branch from the command line is @{u}:

git log --graph --oneline HEAD @{u}
# For Powershell users: @{...} is an operator, you will need to quote "@{u}"
git log --graph --oneline HEAD "@{u}"

You should see the divergence between your current branch and the target branch. See also "More git log options" below.

What can I do to fix this?

This depends on the result you want to reach, and what you see in the history above.

You may want to rebase your commits on top of target commit:

git rebase <some/branch>

# To rebase on top of your default upstream:
git rebase   # The same as 'git rebase @{u}'

You may want to run an actual merge instead of only allowing fast forwards:

git merge <some/branch>
git merge     # The same as 'git merge @{u}'

Or anything that suits your needs:

  • cherry-pick some of your commits on top of the remote branch,
  • use git rebase -i,
  • merge the other way around...

How can I avoid this when I run git pull?

If you have set --ff-only as a default (e.g: if git config pull.ff returns only), you can override this default on a one off basis by explicitly providing a command line flag:

git pull --rebase  # Rebase on top of fetched branch, rather than merge it.
git pull --ff      # Run a normal merge
                   # (note: you *will* have a merge commit in your history)

If you want to change that default to some other value:

# Remove that default value, allow normal merges when pulling
git config --global --unset pull.ff

# Run `git pull --rebase` by default
#   Note: you still need to run 'git config --global --unset pull.ff'
git config --global pull.rebase true

More git log options

To see the differences between two branches in your terminal:

git log --oneline --graph a b will show you the complete histories of a and b combined together. If you want to see the history of a and b since they forked:

git log --oneline --graph --boundary a...b

# a...b (3 dots) : Means 'symmetric difference' in git log
# --boundary     : Will show the commit that was at the fork point
#                  without this option, the two histories will be
#                  printed one below the other

If you want to hide side branches -- for example, if you want to view git log my/branch...master, but you don't want to view the details of all pull requests that got merged in master:

git log --oneline --graph --boundary --first-parent a...b

# --first-parent : On merge commits, only follow the first parent

Many graphical frontend to Git (Git Extensions, TortoiseGit, gitk, etc.) have ways to activate these options when you view your repositories history. Look for the checkboxes in your GUI, and for fields where you may type a...b or HEAD...@{u}.

If you intend to use some of these commands on a regular basis, set an alias for them:

# Example: show HEAD vs @{upstream} log
git config --global alias.whatsup 'log --oneline --graph --boundary HEAD...@{u}'

# You can now run:
git whatsup
Bromoform answered 12/10, 2022 at 6:43 Comment(1)
At least, the advice message will be more complete/informative with Git 2.41 (Q2 2023): See the end of my answerLeninist
M
5

Another option (the one I just used) is to fetch the remote branch and then merge it. I find this often solves the problem when a pull won't work.

$ git fetch <remote> <branch>

$ git merge <remote>/<branch>

I'm surprised the fetch and merge solution was not posted here. If there is a good reason this is not a good approach, perhaps someone will let me know.

Marilynnmarimba answered 1/8, 2023 at 19:31 Comment(0)
C
3

This works for me

git pull --rebase <remote> <branch>
Chalcedony answered 13/8, 2022 at 3:34 Comment(0)
D
3

If origin branch was squashed or there was a change that broke commit history, and if you do not care about your local branch commit history, you can just reset your local branch

git checkout branchname
git reset --hard origin/branchname
Duodecillion answered 4/11, 2022 at 15:52 Comment(0)
D
2

I was facing the same type of issue, and I guess many of us will be facing this when we are pair programming and we also have incoming commits in the branch from other users. These are the steps I faced, and I solved them by git pull --no-rebase:

➜  graphql-tutorial git:(filtering)  git config pull.ff only
➜  graphql-tutorial git:(filtering) git pull
fatal: Not possible to fast-forward, aborting.
➜  graphql-tutorial git:(filtering) git merge
➜  graphql-tutorial git:(filtering)  git config pull.rebase false
➜  graphql-tutorial git:(filtering) git pull
fatal: Not possible to fast-forward, aborting.
➜  graphql-tutorial git:(filtering) git pull --no-rebase
Merge made by the 'ort' strategy.
. <file name>
. <file name>
. <file name>
. <file name>
 5 files changed, 47 insertions(+), 9 deletions(-)
 create mode 100644 app/models/auth_token.rb
Dexedrine answered 20/6, 2022 at 11:41 Comment(0)
A
1

The reason I get this error is that instead of just doing a git checkout, I was doing git checkout -b, creating a branch.

Try redoing the process by simply checking out the remote branch.

Apple answered 3/10, 2022 at 18:16 Comment(3)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Scruggs
I believe it is clear, because it is only a possible solution for those who are trying to solve the problem. I was creating a branch , and not checking it out .. The error kept appearing constantly because it was trying to checkout the branch with the same name, is it clearer now?Apple
I think what is missing is the explanation why creating a branch instead of checking one out can cause the problem, and how it can be solved in this case.Anthracosis
L
-2

If you get this Git error in the future, this solution will help you

An error while take a pull from (development/master/main) to your local branch:

Error message:

hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --re base, --no-re base,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

Solution:

open your .git configuration file and add these lines:

  1. [pull] ff = no

    If step 1 is not working for you then apply step 2.

  2. git pull --ff-only or git config --global pull.ff only

Lubricator answered 19/1, 2023 at 5:9 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Scruggs
W
-5

If you have a commit, try undo it and pull again!

Womanhood answered 9/5, 2022 at 7:48 Comment(1)
''''git pull --rebase'''' will get the job done, no need to un-commit the changes.Pneumatograph

© 2022 - 2024 — McMap. All rights reserved.