本地Git分支需变基但远程无需,是否遗漏更新步骤?
Git本地分支合并master时的分歧问题处理
我本地机器上有一个已更新的Git分支,尝试合并master分支时执行了以下命令:
git pull git fetch origin git pull origin master
出现了要求指定分歧解决方式的错误:
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: ... fatal: Need to specify how to reconcile divergent branches.
但在GitHub网站上,远程分支可以自动合并到master(无需变基),本该与远程保持一致的本地分支却提示需要变基,请问是不是遗漏了某些更新步骤?
Git配置信息
全局配置(git config --global -l)
user.name=<Me> user.email=<Email> http.schannelcheckrevoke=false
本地仓库配置(git config --local -l)
credential.helper=osxkeychain init.defaultbranch=main user.name=<Me> user.email=<Email> http.schannelcheckrevoke=false core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true core.ignorecase=true core.precomposeunicode=true core.hookspath=.husky remote.origin.url=<RemoteUrl> remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* branch.master.remote=origin branch.master.merge=refs/heads/master branch.<*FeatureBranch*>.remote=origin branch.<*FeatureBranch*>.merge=refs/heads/<*FeatureBranch*>
问题原因与解决方法
核心原因
Git 2.27版本之后,默认取消了pull.rebase的预设值,当本地分支与远程分支出现分歧时,Git会要求明确指定用**合并(merge)还是变基(rebase)**来处理。而GitHub的自动合并是在远程服务器执行的,它默认会尝试快进合并,若无法快进则自动创建合并提交,所以不会触发分歧提示。
快速解决当前问题
- 先拉取最新的远程master分支:
git fetch origin master
- 直接将远程master合并到本地分支(和GitHub自动合并逻辑一致):
git merge origin/master
- 无冲突时,Git会自动生成合并提交;
- 有冲突时,手动解决冲突后执行
git add .和git commit完成合并。
避免后续重复出现该错误
设置Git默认的pull行为为合并(与GitHub逻辑对齐):
- 全局生效(所有仓库都采用merge策略):
git config --global pull.rebase false
- 仅当前仓库生效:
git config --local pull.rebase false
内容的提问来源于stack exchange,提问作者Ryan Cocuzzo
相关产品推荐
相关产品推荐

