NOTE: If the commit was published, and the repository is used by others, it is highly recommended NOT to delete the commit. Instead, you can "revert" the commit with the "git revert" command ( see this answer ).
The default to delete or edit an old commit is to use an interactive rebase.
For example, if you want to delete the commit with hash 1ca0fcd:
git rebase -i 1ca0fcd~1
This command will open the VIM with the list of commits after 1ca0fcd (inclusive).
Example of my repository:
pick 1ca0fcd Exposed HttpClient and JsonSerializerSettings through the IJSendClient interface
pick a65278f Updated README to mention CompositeMessageInterceptor
pick cec16d6 Released version 0.3.0
pick 3cdea37 Added link to release notes to nuspecs
pick c862a21 Changed IJSendParser and DefaultJSendParser to use JsonSerializerSettings
# Rebase ddc2139..c862a21 onto ddc2139
#
# 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.
To delete a commit, just delete the commit line (as indicated in the instructions - "If you remove a line here THAT COMMIT WILL BE LOST"). Then type :wq
to save and exit VIM, and interactive rebase will:
- delete commit 1ca0fcd and all commits after 1ca0fcd
- rewrite all commits after 1ca0fcd
It is possible that when re-writing the commits occur conflicts. In this case the rebase will pause, ask you to resolve the conflicts, and then continue using git rebase --continue
.
Read more: 7.6 Git Tools - Rewriting History