Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git revert a commit using a SHA

Tags:

git

How can I revert a commit with a GIVEN SHA? I just want to remove the changes with a given SHA? I want to keep all the commits made BEFORE & AFTER the give SHA. I just want to remove changes of that specified SHA.

I have read Revert to a commit by a SHA hash in Git?, my understanding is that reset all the commits made AFTER the SHA i want to revert. That is not way i want.

like image 822
michael Avatar asked Jun 29 '12 21:06

michael


People also ask

How do you revert a commit using commit hash?

Git Revert: Reverting the Last Git Commit Make sure to enter the code for the hash you want to revert to. The system asks you to enter a specific commit message for the changes the revert command is going to perform. This action creates a new commit based on the one you specified, with a revert tag.

What is SHA in git commit?

The long string following the word commit is called the commit hash. It's unique identifier generated by Git. Every commit has one, and I'll show you what they're used for shortly. Note: The “commit hash” is sometimes called a Git commit “reference” or “SHA”.

How can I revert my commit?

If you want to revert the last commit, you can use git revert head . head refers to the most recent commit in your branch. The reason you use head~1 when using reset is that you are telling Git to "remove all changes in the commits after" ( reset --hard ) "the commit one before head" ( head~1 ).


2 Answers

You can use git revert <commit hash> to try to revert the changes made by the commit. This will not remove the commit from history, just make changes to undo it as a new commit. In other words you will have the first commit still in history, and an additional commit on the head of your branch which is the effective inverse of the original commit.

If you have not yet shared your changes with anyone else, then it is possible to remove the original offending commit from history altogether by using git rebase. There are details in this SO post.

like image 92
Alex Wilson Avatar answered Oct 31 '22 04:10

Alex Wilson


git revert <commit> will attempt to revert a single commit.

It will not change any other commits. You might be confused by git reset which does something entirely different.

For more info: https://www.kernel.org/pub/software/scm/git/docs/git-revert.html

like image 43
Yuval Adam Avatar answered Oct 31 '22 05:10

Yuval Adam