报错现象
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 (the default strategy)
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.
大概原因
在团队开发中,多个开发人员同时在不同的本地环境中对同一个仓库的相同分支进行开发。例如,开发人员 A 和开发人员 B 都从远程仓库的master分支拉取了代码到各自的本地master分支,然后各自进行了一些提交。开发人员 A 先将自己的提交推送到了远程master分支,当开发人员 B 想要git pull远程master分支的最新代码时,就会出现本地master分支和远程master分支的分歧,因为开发人员 B 的本地分支有自己的提交,而远程分支也有开发人员 A 的提交,Git 不知道该如何合并这些不同的提交。
解决
将拉取操作的合并策略设置为默认的merge方式。当执行git pull时,Git 会自动进行合并操作,将远程分支的内容合并到本地分支,可能会产生一个新的合并提交,保留两个分支的提交历史。
git config pull.rebase false
将拉取操作设置为rebase策略。之后执行git pull时,Git 会把本地分支的提交移动到远程分支的最新提交之后,重写提交历史,使提交历史更加线性。
git config pull.rebase true
建议使用rebase保持分支提交记录整洁