[英]git rebase pulls in changes from unrelated branch?
I'm something of a newbie so don't laugh too hard... 我是一个新手,所以不要笑得太厉害......
I did some work on branch A and made a few commits. 我在分支A上做了一些工作并做了一些提交。 Then, had to make a hotfix change so I created branch B and changed a few files.
然后,不得不进行修补程序更改,所以我创建了分支B并更改了一些文件。 Then, because master didn't change I thought it would be simpler and clearer if I did:
然后,因为主人没有改变我认为如果我这样做会更简单和更清楚:
git rebase master
git checkout master
git merge B
But, this applied changes from branch A. What am I not understanding? 但是,这应用了分支A的变化。我不理解什么? This can't be the way it's supposed to work, is it?
这不可能是它应该工作的方式,是吗?
When you created branch B
, you were on branch A
, so the base of your branch B
contains all the work you did in branch A
. 当您创建分支
B
,您在分支A
,因此分支B
的基础包含您在分支A
中执行的所有工作。 This is why those branch A
changes are now in master
. 这就是为什么那些分支
A
变化现在是master
。 Diagramatically, you have the following situation: 从图形上看,您有以下情况:
branchA: A -- B -- C
branchB: A -- B -- C -- D
The correct strategy to making a hotfix in master
would be to create a branch from master
: 在
master
制作修补程序的正确策略是从master
创建一个分支:
# assuming you start on branch B
git checkout master
git checkout -b master_hotfix
# make the fix
git commit -m 'made the hotfix'
git checkout master
git merge master_hotfix
# carry on as you were
git checkout branchB
To undo what you have done, you can try resetting your master
branch to the commit before the erroneous merge took place. 要撤消已完成的操作,可以尝试在发生错误合并之前将
master
分支重置为提交。 Type git log
and count how many commits came in frok the merge with branchB
. 输入
git log
并计算与branchB
合并后提交的提交branchB
。 Then type: 然后输入:
git reset --hard HEAD~N
where N
is the number of commits you wish to remove from master
. 其中
N
是您希望从master
删除的提交数。 Then apply the steps I gave above for correctly making the hotfix. 然后应用我上面给出的步骤正确地创建此修补程序。 Note that you should not use
git reset
if you have already pushsd your local master
to the remote. 请注意,如果您已将本地
master
服务器推送到远程服务器,则不应使用git reset
。 In this case, you could try using git revert
, but it might just make sense to leave master
the way it is, and just merge branchB
again later once you have completex your work there. 在这种情况下,您可以尝试使用
git revert
,但是将master
保持branchB
可能是有意义的,并且只有在您完成工作后再次合并branchB
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.