如何删除主题Git分支的所有提交? [英] How to remove all commits of a topic Git branch?

查看:136
本文介绍了如何删除主题Git分支的所有提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下历史记录:

  A --- B --- C ------- --- D ------------ E master 
\ / \ /
W1 - X1 - Y1 W2 - X2 - Y2
topic1 topic2

是否可以删除所有主题分支及其提交

ABCDE master

解决方案

您解决方案1:壁球合并

一个解决方案是简单地使用硬复位与几个压扁合并:

  git checkout master 
git reset - 硬C

git合并--squash topic1
git commit -m重写D

git合并--squash topic2
git commit -m 重写E

#验证新分支与旧分支没有区别
git diff sha_of_old_E

这里的想法是,压缩合并将复制主题分支的最终状态。最后一次提交)到当前的工作副本中,然后在那里进行提交。它有效地压缩了一个分支到单个提交中。



文档





解决方案2:交互式Rebase



另一种方法是使用交互式底座将主题分支压缩为单个提交。首先,调用rebase

  git rebase -i C 

这会在您的终端上显示交互式底座TODO列表编辑器。你会看到下面的提交显示:

  pick sha W1 
pick sha X1
pick sha Y1
pick sha W2
pick sha X2
pick sha Y2

你想要做的是类似于上面的壁球合并。您可以将主题分支压缩到单个提交中,因此对于主题分支的每次提交,将后代提交到其父项中:

  pick sha W1 
squash sha X1
squash sha Y1
pick sha W2
squash sha X2
squash sha Y2

然后在Vim编辑器中点击:wq 开始交互变基。我不会详细介绍接下来的步骤,因为它们已经在互联网上的其他许多来源中得到了充分的详细说明,例如 Pro Git§ 6.4 Git工具 - 重写历史记录 - 压扁提交



您可能想仔细检查一下,您是否通过验证新重写的分支来正确执行了rebase与以前的使用简单的diff不同:

  git diff sha_of_old_E 

请注意,如果您不打算编辑每个压缩的提交消息,则可以使用fixup,它将重用提交上一次提交的消息:

  pick sha W1 
fixup sha X1
fixup sha Y1
pick sha W2
fixup sha X2
fixup sha Y2

请注意,您也可以分别使用 s f 作为压扁和修正。



文件





强制推送警告



请注意,上述两种解决方案都会重写提交历史记录,因此您的新提交将具有不同的sha ID。如果您已经将旧版本的分支推送到远程,那么您需要强制推送来覆盖旧的副本。如果你与其他人分享你的分支,这可能不是你想要做的事情,因为我不会在这里详细说明原因,因为推动力的潜在危险在很多其他在线资源中都有详细记录,例如: / p>


Suppose I have the following history:

   A---B---C----------D------------E master
            \         /\         /
            W1--X1--Y1 W2--X2--Y2 
                  topic1      topic2

Is it possible to remove all topic branches and their commits as following

A-B-C-D-E master

解决方案

You have a few options for doing this.

Solution 1: Squash Merges

One solution is to simply use a hard reset with a few squash merges:

git checkout master
git reset --hard C

git merge --squash topic1
git commit -m "Rewrite D"

git merge --squash topic2
git commit -m "Rewrite E"

# Verify that the new branch is no different from the old one
git diff sha_of_old_E

The idea here is that a squash merge will copy the final state of a topic branch (i.e. the last commit) into your current working copy, where you can then commit it. It effectively squashes a branch into a single commit.

Documentation

Solution 2: Interactive Rebase

Another method is to use an interactive rebase to squash the topic branches into a single commit. First, invoke the rebase

git rebase -i C

That will bring up the interactive rebase TODO list editor in your terminal. You'll see the following commits displayed:

pick sha W1
pick sha X1
pick sha Y1
pick sha W2
pick sha X2
pick sha Y2

What you want to do is something similar to the squash merge above. You can to squash the topic branches into single commits, so for each commit of a topic branch, squash (or "fixup") the descendant commit into its parent:

pick sha W1
squash sha X1
squash sha Y1
pick sha W2
squash sha X2
squash sha Y2

Then hit :wq in the Vim editor to begin the interactive rebase. I won't go into detail about the next steps, because they're already adequately detailed in other numerous sources around the internet, such as in Pro Git § 6.4 Git Tools - Rewriting History - Squashing Commits.

You'll probably want to double check that you did the rebase correctly by verifying that the newly rewritten branch is no different from the previous one by using a simple diff:

git diff sha_of_old_E

Note that if you'd rather not bother editing the commit messages for each squash, you can use "fixup" instead, which will just reuse the commit message of the previous commit:

pick sha W1
fixup sha X1
fixup sha Y1
pick sha W2
fixup sha X2
fixup sha Y2

Note that you can also use s or f for squash and fixup, respectively.

Documentation

Warning about force pushing

Note that both of the above solutions will rewrite your commit history, of course, so your new commits will have different sha IDs. If you've already pushed the old version of your branch to your remote, then you'll need to force push to overwrite the old copy. This may not be something you want to do if you're sharing your branch with other people, for reasons which I won't detail here, since the possible dangers of force pushing are well documented in numerous other online sources, such as:

这篇关于如何删除主题Git分支的所有提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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