1

如果我想编写应该在这两个分支中的代码,我想知道是否可以同时在两个分支上工作。因此,如果我提交更改,它实际上会更新两个分支。

例如 :

我的分支 A中的代码是这样的

print "I like cake"

我的分支 B中的那个是

print "I love bananas"

但我希望他们都有这行代码:

print "Everyone loves stackoverflow"

我真的必须将它写在分支A,复制它,结帐B,然后粘贴吗?

我不能将更改应用于两个不同的分支吗?

4

1 回答 1

2

If you want to work on two branches simultaneously you should seriously think about why you have separate branches to begin with. Branches are usually there so you have separated commits that do not interfere with the other ones.

That being said you could† cherry-pick the commit on B, once it’s done on branch A. This will reapply the commit on the other branch. I.e. git cherry-pick A to pick the top commit on A and reapply it to the current branch.

†The problem is that it would try to add the diff, where the line print "I like cake" is being removed which is not present on branch B, so the patch would fail and as such also the cherry-pick, resulting in a conflict. And that actually makes sense: How exactly should Git know that those two cake/banana-love lines are actually related to each other?

于 2013-04-16T08:29:33.897 回答