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

  • 6,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 6
  • Tình trạng: Còn hàng

When will I see this message?

When you lập cập git merge --ff-only or git pull --ff-only , and the branch or commit you are trying đồ sộ merge is not based off your current branch—its history has somehow forked from yours.

git pull can have defaults mix in the configuration, sánh you can also see this if you ran a plain git pull origin , and your config has pull.ff = only. To kiểm tra your config: lập cập 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 

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

git log --graph --oneline HEAD @{u}
# For Powershell users: @{...} is an operator, you will need đồ sộ 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 tự đồ sộ fix this?

This depends on the result you want đồ sộ reach, and what you see in the history above.

You may want đồ sộ rebase your commits on top of target commit:

git rebase 

# To rebase on top of your mặc định upstream:
git rebase   # The same as 'git rebase @{u}'

You may want đồ sộ lập cập an actual merge instead of only allowing fast forwards:

git merge 
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 lập cập git pull?

If you have mix --ff-only as a mặc định (e.g: if git config pull.ff returns only), you can override this mặc định on a one off basis by explicitly providing a command line flag:

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

If you want đồ sộ change that mặc định đồ sộ some other value:

# Remove that mặc định value, allow normal merges when pulling
git config --global --unset pull.ff

# Run `git pull --rebase` by default
#   Note: you still need đồ sộ lập cập '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 đồ sộ 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 đồ sộ hide side branches -- for example, if you want đồ sộ view git log my/branch...master, but you don't want đồ sộ 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 đồ sộ Git (Git Extensions, TortoiseGit, gitk, etc.) have ways đồ sộ 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 đồ sộ use some of these commands on a regular basis, mix 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