Git 并在多个分支上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1334027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git and working on multiple branches
提问by Gav
I have a couple of Git branches: 'experimental', 'something' and 'master'.
我有几个 Git 分支:'experimental'、'something' 和 'master'。
I switched to the 'experimental' branch. I noticed a bug which is unrelated to 'experimental' and belongs to changes which have been made in 'something'. How should I fix it?
我切换到“实验”分支。我注意到一个与“实验性”无关的错误,属于“某事”中所做的更改。我该如何解决?
I'm thinking I should switch to 'something', fix the bug, commit and then move back to 'experimental'. How should I take the minor change from 'something' and apply it to both 'master' and 'experimental' so that I don't have to re-fix the bug again when I switch into these branches?
我想我应该切换到“某事”,修复错误,提交,然后回到“实验性”。我应该如何将“某事”的微小更改应用于“主”和“实验”,以便在切换到这些分支时不必再次重新修复错误?
回答by Jakub Nar?bski
There are two solutions not mentioned already that you can use: use a topic branchor use cherry-picking.
您可以使用两种尚未提及的解决方案:使用主题分支或使用cherry-picking。
Topic branch solution
主题分支解决方案
In the topic branch solution, you switch to branch 'something', create a branch to fix a buge.g. 'something-bugfix', merge this branch into 'something' (fixing the bug), then merge this branch into 'experimental'.
在主题分支解决方案中,您切换到分支“something”,创建一个分支来修复错误,例如“something-bugfix”,将此分支合并到“something”(修复错误),然后将此分支合并到“experimental”。
$ git checkout -b something-fix something
[edit, commit]
$ git checkout something
$ git merge something-fix
$ git checkout experimental
$ git merge something-fix
[fix conflicts if necessary and commit]
See also Resolving conflicts/dependencies between topic branches earlyand Never merging back, and perhaps also Committing to a different branchblog posts by Junio C Hamano (git maintainer).
另请参阅尽早解决主题分支之间的冲突/依赖关系和永不合并,也许还可以通过 Junio C Hamano(git 维护者)提交不同的分支博客文章。
Cherry-picking a bugfix
樱桃采摘错误修正
The cherry-pickingsolution is useful if you noticed laterthat the bugfix you created (e.g. on development branch) would be useful also on other branch (e.g. stable branch). In your case you would comit a fix on 'something' branch:
在樱桃采摘,如果你注意到的解决方案是非常有用后来是(在开发分支如)创建的bug修正将是对其他分支(如稳定分支)也是有用的。在您的情况下,您将在“某事”分支上进行修复:
$ git checkout something
[edit, edit, edit]
$ git commit
$ git checkout experimental
Then you noticed that fix you comitted in 'something' branch should be also on 'experimenta' branch. Lets say that this bugfix was commit 'A' (e.g. 'something' if you didn't commit anything on top of 'something', but it might be e.g. 'something~2' or 'c84fb911'):
然后你注意到你在“something”分支中提交的修复也应该在“experimenta”分支上。假设此错误修正是提交“A”(例如,如果您没有在“某事”之上提交任何内容,则为“某事”,但它可能是例如“某事~2”或“c84fb911”):
$ git checkout experimental
$ git cherry-pick A
(you can use --edit
option to git cherry-pickif you want to edit commit message before comitting cherry-picked bugfix).
(如果你想在提交cherry-picked bugfix 之前编辑提交消息,你可以使用--edit
选项git cherry-pick)。
回答by Carmine Paolino
you could:
你可以:
stash
orcommit
the changes you have been working on theexperimental
branchcheckout something
- (optional)
bisect
to find the bug commit
the changescheckout experimental
stash
或commit
您在experimental
分支上所做的更改checkout something
- (可选)
bisect
查找错误 commit
变化checkout experimental
and then:
进而:
rebase something
if you want a clean commit graph (if you expose this repository and you care about that)
rebase something
如果你想要一个干净的提交图(如果你公开这个存储库并且你关心它)
or:
或者:
merge something
if you don't care about 'presentation' :)
merge something
如果你不关心“演示”:)
回答by Vitaly Kushner
since your experimentalbranch has features from somethingyou should do one of the:
因为你的实验分支机构有特色的东西,你应该做的一个:
- merge somethinginto experimentalafter you fix the bug there.
- rebase your experimentalon top of something
- 修复那里的错误后,将某些内容合并到实验中。
- 将你的实验重新建立在某些东西之上
回答by William Pursell
If you can merge, then do so. If you don't want to merge somethinginto the other branches, then fix the bug and cherry-pick that commit into each of the other branches.
如果您可以合并,那么就这样做。如果您不想将某些内容合并到其他分支中,请修复错误并挑选提交到其他每个分支中的内容。
回答by ondra
If you don't progress in 'master' too much, just switch back to the experimental and do 'git merge master'. If you do, I think the 'git cherry-pick' command is your friend.
如果您在“master”方面没有取得太多进展,只需切换回实验并执行“git merge master”。如果你这样做,我认为 'git cherry-pick' 命令是你的朋友。
回答by u0b34a0f6ae
Your branches are somehow related like this:
你的分支在某种程度上是这样相关的:
master > something > experimental
大师>某事>实验
This is, if you really intend to merge something into master.
这是,如果您真的打算将某些内容合并到 master 中。
I would if possible, fix it on top of master, then rebase the others on top of master. But I simply like rebasing. Or you could fix it there and then merge exactly like that
如果可能的话,我会在 master 之上修复它,然后在 master 之上重新调整其他人。但我只是喜欢变基。或者你可以在那里修复它然后完全像那样合并
master > something > experimental
大师>某事>实验
but if something is a simple topic branch, I would not do that. Here it sounds like something is your "candidate next master"
但如果某事是一个简单的主题分支,我不会这样做。这听起来像是你的“候选下一位大师”