当使用pull拉取远程代码时, git报了如何协调分歧的分支错误
当使用git pull拉取远程分支代码时,报了如下的错误提示:
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.rebase false # merge
hint: git config pull.rebase true # rebase
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 --rebase, --no-rebase,
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.
我也不清楚怎么解决,以下是通过AI工具搜索整理到的结果。
根据提示信息有三种解决方式
- git config pull.rebase false
- git config pull.rebase true
- git config pull.ff only
不清楚你系统上配置为何种模式时,可通过下列命令查看。
git config --list
以上命令会显示所有配置,若没看到以上三种模式的任意一种,那说明没配置过。
发生这种错误不好解释, 举例最好说明, 例如远程分支有提交A->B,而你本地又基于A开发了C, 同时提交时就会直接显示上面的错误信息.
当你设置成pull.rebase true模式时, 那么你的代码提交记录会变成菱形结构的, 如下
A---B---C
git会将你本地提交重放至最后, 你只要再在远程重新拉取代码, 两处的提交记录就一致了.
当你设置成pull.rebase false模式时, 那么你的代码提交记录会变成线性的, 如下
C (local)
/ \
A---B---M (remote + merge commit M)
提交日志历史呈现菱形结构并最后多出一个合并提交 M,反映了两个分支在M处汇合了.
当你设置成pull.ff only模式时, 直接报错并终止操作
fatal: Not possible to fast-forward, aborting.
意味着当你执行git pull时,本地分支若与远程分支存在分叉(即双方都有对方没有的新提交),Git不会尝试自动合并或变更合并基础,而是直接报错并中止操作.
三种模式根据你的实际情况来选择. 这里给的建议是:
若是个人开发的话, 最好配置成pull.rebase true模式, 保持直观的线性提交记录
若是团队协作开发的话,最好配置成pull.rebase false模式,会保留完整的分支合并流程,便于出问题时查找到问题的源头
若是团队协作开发而且要求高,出问题需要根据团队规范手动解决的话,就配置成pull.ff only模式



目前共有 2 条回复