Git 并在多个分支上工作

发布于 2024-08-03 12:57:31 字数 213 浏览 5 评论 0原文

我有几个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

最单纯的乌龟 2024-08-10 12:57:32

由于您的experimental分支具有something的功能,您应该执行以下操作之一:

  • 修复后将something合并到experimental那里的错误。
  • 某事之上重新调整你的实验

since your experimental branch has features from something you should do one of the:

  • merge something into experimental after you fix the bug there.
  • rebase your experimental on top of something
望喜 2024-08-10 12:57:32

如果您在“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.

睡美人的小仙女 2024-08-10 12:57:32

您的分支在某种程度上是这样相关的:

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"

甜心 2024-08-10 12:57:32

如果可以合并,那就这样做。如果您不想将某些内容合并到其他分支中,请修复错误并挑选提交到其他每个分支中的内容。

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.

清风不识月 2024-08-10 12:57:32

Git 工作树允许您同时处理多个分支,而无需重新克隆。使用 git worktree add 创建新的工作树,实现不同任务的高效分支切换。

1. 导航到您的项目

  • 打开 Git Bash 并转到您的项目目录。例如:
    cd /path/to/your/project
    

2. 创建新工作树

  • 使用git worktree add命令创建新工作树。例如,如果您的项目位于 C:\projects\the_project 中,并且您想要在 C:\projects\the_project_2 中为名为“feature_branch”的分支创建一个新工作树,请使用:
    git worktree add ../the_project_2 feature_branch
    
  • 这将创建一个新目录“the_project_2”并将“feature_branch”签入其中。

3. 使用新工作树打开 Visual Studio

  • 打开 Visual Studio 并选择新文件夹 (the_project_2) 作为项目目录。
  • 您现在可以在这个新工作树中处理“feature_branch”。

4. 在工作树之间切换

  • 您可以使用标准 Git 命令轻松地在不同工作树之间切换。例如,要切换回原始工作树:
    cd /path/to/your/project
    

5. 管理工作树

  • 列出现有工作树:
    git 工作树列表
    
  • 删除工作树(小心不要丢失重要的更改):
    git worktree 删除 ../the_project_2
    

6. 好处:无需重新克隆

  • 与以前不同,您不必为了切换分支而再次克隆整个项目。

7. 旧版 Visual Studio 的注意事项

  • 某些旧版本的 Visual Studio 可能不完全支持此功能。

请记住将“/path/to/your/project”、“feature_branch”和“../the_project_2”替换为您的实际项目路径、分支名称和所需的工作树路径。这种方法允许您同时在不同的分支上工作,而不需要冗余克隆。

更多信息可以参考链接:
https://www.gitkraken.com/learn/git/git-worktree

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:

  • Open Git Bash and go to your project's directory. For example:
    cd /path/to/your/project
    

2. Create a New Worktree:

  • Use the 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:
    git worktree add ../the_project_2 feature_branch
    
  • This creates a new directory "the_project_2" and checks out the "feature_branch" into it.

3. Open Visual Studio with the New Worktree:

  • Open Visual Studio and choose the new folder (the_project_2) as your project directory.
  • You can now work on the "feature_branch" in this new worktree.

4. Switching Between Worktrees:

  • You can easily switch between different worktrees by using standard Git commands. For example, to switch back to the original worktree:
    cd /path/to/your/project
    

5. Managing Worktrees:

  • To list your existing worktrees:
    git worktree list
    
  • To remove a worktree (be cautious not to lose important changes):
    git worktree remove ../the_project_2
    

6. Benefit: No Re-Cloning Needed:

  • Unlike before, you don't have to clone the entire project again just to switch branches.

7. Note for Older Visual Studio Versions:

  • Some older versions of Visual Studio might not fully support this feature.

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.

For more information can refer the link:
https://www.gitkraken.com/learn/git/git-worktree

深陷 2024-08-10 12:57:31

您可以使用两种尚未提及的解决方案:使用主题分支或使用择优挑选


主题分支解决方案

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

$ 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 维护者)的博客文章。


择优挑选 bug 修复

如果您后来注意到您创建的 bug 修复(例如在开发分支上)在其他分支(例如稳定分支)。在您的情况下,您将在“something”分支上提交修复:

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

然后您注意到您在“something”分支中提交的修复也应该在“experimenta”分支上。可以说这个错误修复是提交“A”(例如,如果您没有在“某事”之上提交任何内容,则为“某事”,但它可能是例如“某事〜2”或“c84fb911”):(

$ git checkout experimental
$ git cherry-pick A

您可以使用< 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'.

$ 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 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:

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

$ git checkout experimental
$ git cherry-pick A

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

别闹i 2024-08-10 12:57:31

您可以:

  • 存储提交您在实验分支上进行的更改
  • 签出某些内容
  • (可选)< code>bisect 来查找 bug
  • commit 更改
  • checkoutexperimental

然后:

  • rebase Something 如果你想要一个干净的提交图(如果您公开此存储库并且您关心它)

或者:

  • 合并某些内容如果您不关心“演示”:)

you could:

  • stash or commit the changes you have been working on the experimental branch
  • checkout something
  • (optional) bisect to find the bug
  • commit the changes
  • checkout 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' :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文