Welcome to Abdul Malik Ikhsan's Blog

Practical GIT (3) : Remove Specific commits

Posted in GIT, Teknologi by samsonasik on February 24, 2013

git-logo If you’are working with pilot project that many feature are experiment conditions, you need this feature, You can remove commits that you don’t want to exist in your revisions log. This feature named rebase. You can find the whole commits lists on range by typing :

git rebase -i HEAD~5

that command will show you last 5 commit and show them in the editor like the following :

pick 5c22314 third commit
pick 8de60b5 fourth commit
pick 9d3556f fifth commit
pick 434ddc3 sixth commit
pick d9b917g seventh commit

# Rebase d9b917f..8de60b5 onto d9b917f
# 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
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.


If you want to remove fifth and sixth commit, just remove these two lines and save the editor ( if you’re working with vim, type ‘dd’ in the line you want to delete, and type ‘:wq!’. After do that, check by typing command :

git log

and you will get the result like the following ( fifth and sixth commit deleted ! )

commit d9b917g9d3556cfb0d0bbcd8ecd952cf4f358eea
Author: Abdul Malik Ikhsan <samsonasik@gmail.com>
Date:   Wed Feb 20 15:47:13 2013 +0700

    seventh commit

commit 8de60b59d3556cd8eeafb0d0bbcd8ec952cf4f35
Author: Abdul Malik Ikhsan <samsonasik@gmail.com>
Date:   Wed Feb 20 15:47:13 2013 +0700

    fourth commit

commit 5c223149d3556cd952cf4f358eeafb0d0bbcd8ec
Author: Abdul Malik Ikhsan <samsonasik@gmail.com>
Date:   Wed Feb 20 15:47:13 2013 +0700

    third commit

commit 5c2231434ddc3bac911ab38a22d2b47f5736f7d4
Author: Abdul Malik Ikhsan <samsonasik@gmail.com>
Date:   Wed Feb 20 15:46:50 2013 +0700

    second commit

commit d9b917f6a48eb9c0c415772f70b7c66adf3de0b5
Author: Abdul Malik Ikhsan <samsonasik@gmail.com>
Date:   Wed Feb 20 15:45:26 2013 +0700

    first commit

With rebase, you can combine commits to/from other branch or squash commits with commands list while you get the list of commit via rebase -i command.

references :
1. http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: