How to remove an old commit

3

I'm trying to remove a very old commit from my repository that was just to test git, I researched some solutions and got into it:

git rebase --onto (sha1) (sha2)

or

git rebase --onto HEAD~2 HEAD~1

But I get some errors, my question is if there is any kind of "default" to remove a specific commit.

    
asked by anonymous 06.04.2015 / 22:31

2 answers

5

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

    
07.04.2015 / 10:17
0

I found an explanation that I believe can help you, but I could not test it, I recommend that you create a repository with branchs and perform a test, because they are commands that you have to be very careful to execute them.

1 - Check out the previous commit to which you want to delete (to find the commit you can use git log for example):

Ex: git checkout b3d92c5 

2- Create a new branch:

Ex: git checkout -b reparo

3- Use the cherry-pick command by passing the commit you want to delete:

Ex: git cherry-pick 77b9b82 

4 - Go back to the master branch

Ex: git checkout master 

5- Aim / Reset master for last commit

Ex: git reset --hard b3d92c5 

6- Perform a merge with the repair branch created earlier

Ex: git merge reparo

7- Push the master to the remote repository

Ex: git push --hard origin master 

Source with examples and more details

    
06.04.2015 / 23:50