The below walkthrough explains how to cleanup some commits by removing them from the history. You would want to do such drastic action if you need to completely remove a commit, as if it never existed – losing all the edits it introduced.
This is our WIP/feature branch, and we will be using rebase to remove some commits after a503773. So let’s check the commits first
$ git log --oneline --graph --all * a8f5ed0 (HEAD -> wip) add line 11:10 to file1104 * 2336293 add line at 10:55 that will be deleted later * b7b2eff add line to notes file * a503773 edits to notes 9:48 * 61ac8b3 edits to notes 9:47
Let’s delete the second last commit (2336293) with comment “add line at 10:55 that will be deleted later”
$ git rebase -i a503773
The next step is to tell Git what to do during the rebase. Git will open an editor like this, listing all 3 commits after a503773, in reverse order:
pick b7b2eff add line to notes file pick 2336293 add line at 10:55 that will be deleted later pick a8f5ed0 add line 11:10 to file1104 # Rebase a503773..a8f5ed0 onto a503773 (3 commands)
If we edit content in the open editor such as below – Git will drop the commit 2336293
pick b7b2eff add line to notes file d 2336293 add line at 10:55 that will be deleted later pick a8f5ed0 add line 11:10 to file1104 # Rebase a503773..a8f5ed0 onto a503773 (3 commands)
When we Save and Quit the editor:
Successfully rebased and updated refs/heads/wip.
$ git log --oneline --graph --all * 31a960d (HEAD -> wip) add line 11:10 to file1104 * b7b2eff add line to notes file * a503773 edits to notes 9:48 * 61ac8b3 edits to notes 9:47
As we can see there’s no more “2336293 add line at 10:55 that will be deleted later”