How to remove an old commit

Asked

Viewed 23,279 times

4

I’m trying to remove a very old commit from my repository that was just to test git, I researched some solutions and came up with this:

git rebase --onto (sha1) (sha2)

or

git rebase --onto HEAD~2 HEAD~1

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

  • I thought the two were the same command, only with different types of instructions, in which case errors you get in each of the commands?

  • Oh it gives some conflict errors, I think the old commit files don’t even exist anymore, but in case of the question I wanted to know if this is the most usual method to delete a commit

  • So, as I said, as far as I know both commands are the same, only differentiates by the type of instruction you went through to identify, the first I think is safe and the other is normal, but the instruction is the same.

2 answers

8


NOTE: If the commit has been posted, and the repository is used by others, it is highly recommended NOT to delete the commit. Instead, you can "reverse" the commit with the "git revert" command" (see this response).


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 1ca0fcd hash:

git rebase -i 1ca0fcd~1

This command will open VIM with the commit list after 1ca0fcd (inclusive). Example of a repository of mine:

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, simply delete the line from that commit (as indicated in the instructions - "If you remove a line here THAT COMMIT WILL BE LOST"). Then write :wq to record and exit VIM, and the interactive rebase will:

  • delete commit 1ca0fcd and all commits after 1ca0fcd
  • rewrite all commits after 1ca0fcd

It is possible that when re-writing commits conflicts occur. In this case the rebase will pause, ask you to resolve conflicts, and then continue using git rebase --continue.

Read more: 7.6 Git Tools - Rewriting History

  • much simpler than I imagined

  • I could not delete the line by the path, the cursor went through the entire line and did not remove anything, which can be?

1

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

1-Check out the commit 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-Point/Reset the master to the last commit

Ex: git reset --hard b3d92c5 

6-Merge with the previously created repair branch

Ex: git merge reparo

7- Push the master to the remote repository

Ex: git push --hard origin master 

Source (English) with examples and details

Browser other questions tagged

You are not signed in. Login or sign up in order to post.