Remove local commit completely from tree

2020-05-03 10:44发布

问题:

I have 5 local unpushed commits and I want to delete commit #3 from the tree, so parent of commit #4 would point to commit #2

How would I do that?

An image describing what I mean:

回答1:

you can use git rebase -i :

git rebase -i HEAD~5

allows you to interactively choose which of the last (5 in this case) commits you wish to use, edit, squash,...

You will get a file to edit; like this:

pick a58f195 commit 1
pick abe6821 commit 2
pick f74035d commit 3
pick 3f171df commit 4
pick afa24a9 commit 5

in which you will just delete the line pointing to the commit you don't want anymore.. Note that you can do much more with the interactive rebase :)