How to remove all commits of a topic Git branch?

拜拜、爱过 提交于 2019-12-06 04:18:31

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:

VonC

One trick would be to clone a single branch from that repo into a new repo

git clone <url> --branch <branch> --single-branch [<folder>]

And use the new repo from there.

You could use interactive rebase:

git rebase -i <hash of A>

and then delete the commits you don't want by just deleting the lines referring to the commits. You will have something like:

pick <hash of B> message of B
pick <hash of C> message of C
pick <hash of W1> message of W1
pick <hash of X1> message of X1
pick <hash of Y1> message of Y1
pick <hash of D> message of D
pick <hash of W2> message of W2
pick <hash of X2> message of X2
pick <hash of Y2> message of Y2
pick <hash of E> message of E

Then delete those lines referring to the topic branches like this:

pick <hash of B> message of B
pick <hash of C> message of C
pick <hash of D> message of D
pick <hash of E> message of E
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!