首页 > 解决方案 > 您如何设置分支出现分歧的情况?

问题描述

我正在尝试重现分支出现分歧的情况(这样我就可以练习解决问题),正如这个问题中提到的那样:master 分支和“origin/master”已经分歧,如何“取消分歧”分支?

关于如何创建它的任何示例?

标签: git

解决方案


一个。创建两个本地分支:

git init
git checkout -b master
echo aaa > a.txt && git add a.txt && git commit -m "first commit"

git checkout -b diverge
echo bbb > b.txt && git add b.txt && git commit -m "diverge: bbb"
echo ccc > c.txt && git add c.txt && git commit -m "diverge: ccc"

# return to 'master' branch (which is 2 commits behind 'left') :
git checkout master
echo ddd > d.txt && git add d.txt && git commit -m "master: ddd"
echo eee > e.txt && git add e.txt && git commit -m "master: eee"


# now 'master' and 'diverge' are two local branches, which diverge :
git log --oneline --graph diverge master

湾。如果您想要一个发散的遥控器,请将“发散”分支作为“主”分支推送到原点:

git push origin diverge:refs/heads/master
# you now have a 'origin/master' branch

# at the same time, set your local 'master' branch to track 'origin/master' :
git branch -u master origin/master

# now 'git status' on master should say 'ahead 2, behind 2'
git checkout master
git status

创建不同历史的其他方法:

  • 创建一个 master 分支,将其推送到 orgin,然后运行git rebase -i HEAD~3例如,并编辑或删除第一个提交

  • 创建一个主分支,推送到原点,然后git reset将您的主分支推送到过去的某个提交,然后创建新的提交master


推荐阅读