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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于您的experimental分支具有something的功能,您应该执行以下操作之一:
since your experimental branch has features from something you should do one of the:
如果您在“master”方面没有取得太多进展,只需切换回实验并执行“git merge master”即可。如果您这样做,我认为“gitcherry-pick”命令是您的朋友。
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 >某事>实验性
的这是,如果你真的打算将某些东西合并到master中。
如果可能的话,我会在 master 之上修复它,然后在 master 之上重新调整其他的基础。但我只是喜欢变基。或者你可以在那里修复它,然后完全像
master > 那样合并。某事>实验性的
,但如果某件事是一个简单的主题分支,我不会这样做。这听起来像是你的“下一任主人的候选人”
Your branches are somehow related like this:
master > something > experimental
This is, if you really intend to merge something into 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 > 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"
如果可以合并,那就这样做。如果您不想将某些内容合并到其他分支中,请修复错误并挑选提交到其他每个分支中的内容。
If you can merge, then do so. If you don't want to merge something into the other branches, then fix the bug and cherry-pick that commit into each of the other branches.
Git 工作树允许您同时处理多个分支,而无需重新克隆。使用 git worktree add 创建新的工作树,实现不同任务的高效分支切换。
1. 导航到您的项目:
2. 创建新工作树:
git worktree add
命令创建新工作树。例如,如果您的项目位于 C:\projects\the_project 中,并且您想要在 C:\projects\the_project_2 中为名为“feature_branch”的分支创建一个新工作树,请使用:3. 使用新工作树打开 Visual Studio:
4. 在工作树之间切换:
5. 管理工作树:
6. 好处:无需重新克隆:
7. 旧版 Visual Studio 的注意事项:
请记住将“/path/to/your/project”、“feature_branch”和“../the_project_2”替换为您的实际项目路径、分支名称和所需的工作树路径。这种方法允许您同时在不同的分支上工作,而不需要冗余克隆。
Git worktrees allow you to work on multiple branches simultaneously without re-cloning. Use git worktree add to create a new worktree, enabling efficient branch switching for different tasks.
1. Navigate to Your Project:
2. Create a New Worktree:
git worktree add
command to create a new worktree. For instance, if your project is in C:\projects\the_project and you want to create a new worktree at C:\projects\the_project_2 for a branch named "feature_branch," use:3. Open Visual Studio with the New Worktree:
4. Switching Between Worktrees:
5. Managing Worktrees:
6. Benefit: No Re-Cloning Needed:
7. Note for Older Visual Studio Versions:
Remember to replace "/path/to/your/project," "feature_branch," and "../the_project_2" with your actual project path, branch name, and desired worktree path. This approach allows you to work on different branches simultaneously without the need for redundant cloning.
您可以使用两种尚未提及的解决方案:使用主题分支或使用择优挑选。
主题分支解决方案
在主题分支解决方案中,您切换到分支'something',创建一个分支来修复错误例如'something-bugfix',将此分支合并到'某事”(修复错误),然后将此分支合并到“实验”中。
另请参阅尽早解决主题分支之间的冲突/依赖性和永远不会合并回来,也许还有 致力于不同的分支 Junio C Hamano(git 维护者)的博客文章。
择优挑选 bug 修复
如果您后来注意到您创建的 bug 修复(例如在开发分支上)在其他分支(例如稳定分支)。在您的情况下,您将在“something”分支上提交修复:
然后您注意到您在“something”分支中提交的修复也应该在“experimenta”分支上。可以说这个错误修复是提交“A”(例如,如果您没有在“某事”之上提交任何内容,则为“某事”,但它可能是例如“某事〜2”或“c84fb911”):(
您可以使用< code>--edit 选项 gitcherry-pick(如果您想在提交cherry-picked bugfix之前编辑提交消息)。
There are two solutions not mentioned already that you can use: use a topic branch or use cherry-picking.
Topic branch solution
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'.
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).
Cherry-picking a bugfix
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:
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'):
(you can use
--edit
option to git cherry-pick if you want to edit commit message before comitting cherry-picked bugfix).您可以:
存储
或提交
您在实验
分支上进行的更改签出某些内容
commit
更改checkoutexperimental
然后:
rebase Something
如果你想要一个干净的提交图(如果您公开此存储库并且您关心它)或者:
合并某些内容
如果您不关心“演示”:)you could:
stash
orcommit
the changes you have been working on theexperimental
branchcheckout something
bisect
to find the bugcommit
the changescheckout experimental
and then:
rebase something
if you want a clean commit graph (if you expose this repository and you care about that)or:
merge something
if you don't care about 'presentation' :)