Git 并在多个分支上工作 [英] Git and working on multiple branches

查看:15
本文介绍了Git 并在多个分支上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 Git 分支:experimental"、something"和master".

I have a couple of Git branches: 'experimental', 'something' and '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?

推荐答案

有两种尚未提及的解决方案可供您使用:使用 topic 分支 或使用 cherry-picking.

There are two solutions not mentioned already that you can use: use a topic branch or use cherry-picking.

主题分支解决方案中,您切换到分支某事",创建一个分支来修复错误,例如'something-bugfix',将此分支合并到'something'(修复错误),然后将此分支合并到'experimental'.

In the topic branch solution, you switch to branch 'something', create a branch to fix a bug e.g. 'something-bugfix', merge this branch into 'something' (fixing the bug), then merge this branch into '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]

另见尽早解决主题分支之间的冲突/依赖永不合并,也许还有 提交给不同的分支 由 Junio C Hamano(git 维护者)撰写的博文.

See also Resolving conflicts/dependencies between topic branches early and Never merging back, and perhaps also Committing to a different branch blog posts by Junio C Hamano (git maintainer).

樱桃采摘解决方案非常有用,如果您稍后注意到您创建的错误修复(例如在开发分支上)在其他分支(例如稳定分支)上也很有用).在您的情况下,您会在某事"分支上进行修复:

The cherry-picking solution is useful if you noticed later that 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:

$ git checkout something
[edit, edit, edit]
$ git commit
$ git checkout experimental

然后你注意到你在'something'分支中提交的修复也应该在'experimenta'分支上.假设此错误修正是提交A"(例如,如果您没有在某事"之上提交任何内容,则为某事",但它可能是例如某事~2"或c84fb911"):

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'):

$ git checkout experimental
$ git cherry-pick A

(您可以使用 --edit 选项来git cherry-pick.

(you can use --edit option to git cherry-pick if you want to edit commit message before comitting cherry-picked bugfix).

这篇关于Git 并在多个分支上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆