最近有git pull后频繁出现再次提交失败的问题。
异常提示
Your branch and 'origin/otherNormalBranch' have diverged,
and have x and y different commits each, respectively.
根本原因
研究了一下根本原因在于:本地分支 AA pull之后始终提示和远程分支BB有分歧。
但常规来讲,在本地分支pull 分支A时,应该直接呈现出将分支更新完毕的提示。
//正常的场景,本地分支pull会反馈本地分支的情况
server123123@4060:~/worksheet$ git checkout targetNormalBranch //切换到正常的目标分支
Updating files: 100% (1579/1579), done.
Switched to branch 'targetNormalBranch'
Your branch is up to date with 'origin/targetNormalBranch'. //输出提示也符合预期
//异常的场景,本地pull分支1,会反馈和远端 分支2的分歧
server123123@4060:~/worksheet$ git checkout targetErrorBranch //切换到异常的目标分支
Updating files: 100% (1579/1579), done.
Switched to branch 'targetErrorBranch' //虽然显示本地分支名正确
Your branch and 'origin/otherNormalBranch' have diverged, //但会提示和远程仓库的另一条分支有分歧
and have 5 and 448 different commits each, respectively.
>>>如上所述,怀疑点为,本地仓是否绑定到了错误的远程分支上
问题检查
通过git branch -vv,查看当前本地分支和远程分支的绑定关系,发现有绑定出错
server123123@4060:~/worksheet$ git branch -vv
targetErrorBranch [origin/otherNormalBranch: ahead 5, behind 448] //这一分支果然绑定错误
* targetNormalBranch [origin/targetNormalBranch]
otherNormalBranch [origin/otherNormalBranch]
解决方案
需要将远程分支修正为想要的目标分支,
git branch --set-upstream-to=origin/AAA AAA
这条语句可以将当前的AAA分支 重设 为远端的AAA,注意后一个AAA是本地分支名,前一个origin/AAA是在远端的分支名
server123123@4060:~/worksheet$ git branch --set-upstream-to=origin/targetErrorBranch targetErrorBranch
branch 'targetErrorBranch' set up to track 'origin/targetErrorBranch'.
server123123@4060:~/worksheet$ git branch -vv
targetErrorBranch [origin/targetErrorBranch]
* targetNormalBranch [origin/targetNormalBranch]
otherNormalBranch [origin/otherNormalBranch]