前言
今天想把自己网站的源代码上传到GitHub仓库里保存,这样之后也可以在多台电脑上操作了。但用Git push到GitHub时出错了,我想应该是之前把这个源代码在不同的电脑上到处拷贝操作上传导致版本信息不一致导致的。所以有了如下操作及解决过程。
过程
1 | Last login: Wed Feb 1 13:57:43 on console |
解决思路
Fatal: Not possible to fast-forward, aborting
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.Maybe try
1
git pull origin <branch> --rebase
“You have divergent branches and need to specify how to reconcile them.” #14431
Working solution:
@aenzor This is a message that started showing up in Git v2.27 in the command line. You can choose one of the options mentioned in the hints to stop it from occurring. The git config pull.rebase false is the default setting. You can read more about the settings in the official Git docs. GitHub Desktop will respect your choice in the global .gitconfig.
Possible solution:
We are looking at a more ideal solution for this through Desktop, but in the mean time. For those unfamiliar with using the Command Line Interface (CLI).
From GitHub Desktop, you can press Ctrl + ` (Also available from the “Repository” main menu as “Open in [Your set terminal]”). This should open up a CLI.
There you can type:
git config --global pull.ff true
(or any of the other options specified in the error hints).Now when you attempt to pull it will use that configuration and allow you to continue.
There is the possibility that because you do not use the CLI, you may need to download and install git for this to work.
Some explanation of choice above if interested:
I suggestedpull.ff true
simply because it attempts to fast forward your branch to be up to date with your remote before applying your local commits and if not it will preform a merge from the remote to your local branch. Read docs here. Typically when you pull a branch, add commits, and push. It is in the order assuming your local is up to date with remote. By merging when fast forwarding is not possible, you will see a merge commit informing you of how it was handled. (The other options are to rebase or always merge, many new users find rebase to be less intuitive, but really accomplishes the same thing)I added the –global flag so that your choice will apply to all your repos and you won’t see this error message again. Simply omit this if you want a different behavior per repository.
How can I deal with this Git warning? “Pulling without specifying how to reconcile divergent branches is discouraged”
In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.
When you do a git pull origin master,
git pull performs a merge, which often creates a merge commit. Therefore, by default, pulling from the remote is not a harmless operation: it can create a new commit SHA hash value that didn’t exist before. This behavior can confuse a user, because what feels like it should be a harmless download operation actually changes the commit history in unpredictable ways.To avoid this, you need
git pull –ff-only
(or not? read on to see which one fits your requirements)With git pull –ff-only, Git will update your branch only if it can be “fast-forwarded” without creating new commits. If this can’t be done, git pull –ff-only simply aborts with an error message.
You can configure your Git client to always use –ff-only by default, so you get this behavior even if you forget the command-line flag:
git config –global pull.ff only
Note: The –global flag applies the change for all repositories on your machine. If you want this behaviour only for the repository you’re in, omit the flag.
Terminal stuck when pushing to GitHub?
1
2# Restart your ssh agent!
killall ssh-agent; eval `ssh-agent`
总结
换电脑或者版本信息不一致时,建议走如下流程
1 | # main: my branch, replace main or switch to others based your situation |
Reference
- Git push hangs when pushing to Github?
- Reconcile divergent branches #14423
- “You have divergent branches and need to specify how to reconcile them.” #14431
- How can I deal with this Git warning? “Pulling without specifying how to reconcile divergent branches is discouraged”
- Fatal: Not possible to fast-forward, aborting
About this Post
This post is written by Andy, licensed under CC BY-NC 4.0.