从 Git 中的单个分支中删除提交

发布于 2024-12-02 10:59:12 字数 1219 浏览 1 评论 0原文

我有两个分支,master 和 feature1。当我意识到我需要做其他不相关的事情时,我正在研究功能 1。不幸的是,我忘记从 master 分支,而是从 feature1 创建了我的 feature2 分支。现在我想将 feature2 合并到 master 中,但不能,因为该分支中有 feature1 的部分。

如何从 feature2 分支中删除 feature1 提交?它涉及变基吗?

我觉得如果我可以将 feature2 的起始参考点更改为 master 所在的位置,这可能会有所帮助,但不知道如何操作。

编辑:

谢谢您的回答!我尝试根据@Mark的解决方案进行变基,但意识到历史比我最初想象的要复杂。 feature 2 已合并到其他功能分支中,master 曾一度合并到 feature2 中。 master 上还有与 feature2 无关的其他提交,一切仍然是本地的。

真的,我的历史更像是这样的:

A - B - C - D - - - - - - - - - - - - - L - M  master  
            |                          
            |               - I - J - K  feature2                  
            \              /           \
             - E - F - G - H - - - - - -N - O - P  feature1

我想要的是这样的:

A - B - C - D - - - - - - - - - -  L - M  master
            |\                          
            |  - - - - - I - J - K  feature2                  
            \                     \
              - E - F - G - H - - - N - O - P  feature1

我也尝试过:

git rebase --onto 1524b824cfce5856a49e feature1 feature2
// 1524b824cfce5856a49e == D

但这只是将分支名称功能 2 设置为指向 1524,并将提交 I、J、K 与其原始父级一起留下。

I have two branches, master and feature1. I was working on feature1 when I realized that I needed to work on something else unrelated. Unfortunately, I forgot to branch from the master, and instead created my feature2 branch from feature1. Now I want to merge feature2 into master, but cannot because there are parts of feature1 in that branch.

How do I remove the feature1 commits from the feature2 branch? Does it involve rebasing?

I feel that if I could change the starting reference point of feature2 to be where master is, that might help, but have no idea how.

EDIT:

Thank you for the answers! I tried rebasing according to @Mark's solution, but realized that the history is more complicated than I originally thought. feature 2 has been merged into other feature branches, and master was merged into feature2 at one point. There are also additional commits on master that are not related to feature2 Everything is still local.

Really, my history is more like this:

A - B - C - D - - - - - - - - - - - - - L - M  master  
            |                          
            |               - I - J - K  feature2                  
            \              /           \
             - E - F - G - H - - - - - -N - O - P  feature1

And what I want is this:

A - B - C - D - - - - - - - - - -  L - M  master
            |\                          
            |  - - - - - I - J - K  feature2                  
            \                     \
              - E - F - G - H - - - N - O - P  feature1

I have also tried:

git rebase --onto 1524b824cfce5856a49e feature1 feature2
// 1524b824cfce5856a49e == D

But this just sets the branch name feature 2 pointing at 1524, and leaves commits I, J, K with their original parents.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

白芷 2024-12-09 10:59:12

如果只有您在 feature2 分支上工作并且尚未与其他人共享它,则可以在合并之前对该分支进行变基。例如,您可以执行以下操作:

git checkout feature2
git rebase --onto master feature1 feature2

...这将重写您的 feature2 分支,使其包含 feature2 中的所有提交,因为它是从feature1,但重新应用于master。我建议您随后使用 gitk --all 或类似的 git 历史查看器来检查此操作的结果是否是您想要的。

顺便说一句,这正是 git rebase 文档 - 请参阅段落开头:

以下是如何将基于一个分支的主题分支移植到另一个分支,假装您使用 rebase --onto 从后一个分支分叉了主题分支。
[...]


针对问题中的更多材料进行了更新:

从您的历史记录开始,如下所示:

A - B - C - D - - - - - - - - - - - - - L - M  master  
            |                          
            |               - I - J - K  feature2                  
            \              /           \
             - E - F - G - H - - - - - -N - O - P  feature1

您可以通过执行以下操作来获得您想要的内容:

git checkout feature2
git rebase --onto D H feature2

这将为您留下以下历史记录 :看起来像:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master  
            |\
            | \ - I' J' K' feature2       I - J - K                    
            \                           /           \
             - - - - - - - E - F - G - H - - - - - -N - O - P  feature1

feature1 中创建所需的合并之前,您应该记下 OP 的 SHA1sum。 (我假设 N 只是正常合并,而不是“邪恶合并”。)

然后进行硬重置,将 feature1 移回 H

git checkout feature1
git rest --hard H

那么您应该有历史记录:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master  
            |\
            | \ - I' J' K' feature2
            \
             - - - - - - - E - F - G - H feature1

现在你显然想将 K' 合并到 feature1 中:

git merge K'

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master  
            |\
            | \ - I' - J' - - - - - - - K' feature2
            \                             \
             - - - - - - - E - F - G - H - Z feature1

最后你可以挑选旧的 OPfeature1 上:

git cherry-pick O
git cherry-pick P

... 到 离开:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master  
            |\
            | \ - I' - J' - - - - - - - K' feature2
            \                            \
             - - - - - - - E - F - G - H - Z - - O' - P' feature1

If it's only you working on your feature2 branch and you haven't shared it with other people, it's fine to rebase that branch before merging. You could do the following, for example:

git checkout feature2
git rebase --onto master feature1 feature2

... which will rewrite your feature2 branch, leaving it so that it consists of all the commits in feature2 since it was branched from feature1, but reapplied onto master. I suggest you use gitk --all or a similar git history viewer afterwards to check that the result of this operation is what you want.

Incidentally, this is exactly the scenario used to explain --onto in the git rebase documentation - see the paragraph beginning:

Here is how you would transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto.
[...]


Updated in response to more material in the question:

Starting with your history, which looks like this:

A - B - C - D - - - - - - - - - - - - - L - M  master  
            |                          
            |               - I - J - K  feature2                  
            \              /           \
             - E - F - G - H - - - - - -N - O - P  feature1

You can get what you want by doing the following:

git checkout feature2
git rebase --onto D H feature2

This will leave you with history that looks like:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master  
            |\
            | \ - I' J' K' feature2       I - J - K                    
            \                           /           \
             - - - - - - - E - F - G - H - - - - - -N - O - P  feature1

Before creating the merge that you want in feature1, you should note down the SHA1sum of O and P. (I'm assuming N is just a normal merge, and not an "evil merge".)

Then do a hard reset to move feature1 back to H:

git checkout feature1
git rest --hard H

Then you should have the history:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master  
            |\
            | \ - I' J' K' feature2
            \
             - - - - - - - E - F - G - H feature1

Now you apparently want to merge K' into feature1:

git merge K'

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master  
            |\
            | \ - I' - J' - - - - - - - K' feature2
            \                             \
             - - - - - - - E - F - G - H - Z feature1

And finally you can cherry-pick the old O and P onto feature1 with:

git cherry-pick O
git cherry-pick P

... to leave:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master  
            |\
            | \ - I' - J' - - - - - - - K' feature2
            \                            \
             - - - - - - - E - F - G - H - Z - - O' - P' feature1
够钟 2024-12-09 10:59:12

如果您不介意将 feature2 合并回 P 而不是 M,这里有一个相对简单的解决方案。

  1. Rebase IJK 到 D,查看部分标题“更有趣的 rebase
  2. 将 OP 重新设置为 H。与步骤 1 中的方式相同
  3. 最后使用 git merge feature2 将 feature2 合并到 feature1,同时拥有 feature1 已签出。

如果您希望将 K 合并到特定提交中,则变基会变得有点棘手。

If you don't mind that feature2 is merged back in on P instead of M, here is a relatively simple solution.

  1. Rebase I-J-K onto D, look at the section title "more interesting rebases
  2. Rebase O-P onto H. same way as done in step 1
  3. Finally merge feature2 into feature1 with git merge feature2 while having feature1 checked out.

If you want K merged into a particular commits, the rebasing gets a little trickier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文