Skip to content

Instantly share code, notes, and snippets.

@originalhat
Last active December 29, 2016 23:05
Show Gist options
  • Save originalhat/5258119 to your computer and use it in GitHub Desktop.
Save originalhat/5258119 to your computer and use it in GitHub Desktop.
using rebase to remove a commit

Using Rebase to Remove Bad Commits

Rebase at your own risk!

Fixing up and squashing commits with rebase is great fun and handy to keep the logs clean from lots of little commits like typos, but only before a push (unless you're sure nobody else has pulled since then).

When you rebase, it re-writes all SHA values after the removed commit(s). This can be troublesome when you may have already pushed to a remote. This has serious implications for breaking the repository for collaborators.

Create & Enter the Branch

$ git b some-branch master
$ git co some-branch

Make Some Commits

# git log
* e0bf141 (HEAD, master) commit file3. Devin Brown, 1 second ago
* 16bd64c commit file2. Devin Brown, 8 seconds ago
* 5f66d5b commit file1. Devin Brown, 14 seconds ago

Remove a Specific Commit

Lets remove commit 16bd64c, which contains "file2".

$ git rebase -i some-branch~3

Note: 3 represents how many commits you would like to view for rebasing.

pick 5f66d5b commit file1
pick 16bd64c commit file2
pick e0bf141 commit file3

# Rebase 679fc6e..e0bf141 onto 679fc6e
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Important! Delete the entire line of the commit you would like to be removed.

This will look something like this...

pick 5f66d5b commit file1
pick e0bf141 commit file3

# Rebase 679fc6e..e0bf141 onto 679fc6e
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

To abort the rebase at any time, remove all lines from the rebase log.

Save and Exit

For the rebase to take effect, save and close the window in which you were "rebasing".

Successfully rebased and updated refs/heads/some-branch.

Success

We can now see that the commit for "file2" is now gone.

# git log
* b2bbb4c (HEAD, master) commit file3. Devin Brown, 8 minutes ago
* 5f66d5b commit file1. Devin Brown, 8 minutes ago
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment