Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "un-revert" a reverted Git commit?

Tags:

git

undo

revert

Given a change that has been committed using commit, and then reverted using revert, what is the best way to then undo that revert?

Ideally, this should be done with a new commit, so as to not re-write history.

like image 472
JimmidyJoo Avatar asked Jan 04 '12 13:01

JimmidyJoo


People also ask

Can I revert reverted commit?

You can only reset to a previous commit and ignore the local changes, or revert the changes.

How do I undo a revert pull request?

Just revert the revert. So by clicking the revert button you will have created a new PR (your step 2). Once this is merged, you will have the option to revert this, which will create a new branch with all your changes back in. You can then pull this, make changes to it (if needed) and create a new PR.

How do you push a reverted commit?

If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit. To fix the detached head do git checkout <current branch> .

How to revert a commit in Git?

A revert commit is just like any other commit in git. Meaning, you can revert it, as in: git revert 648d7d808bc1bca6dbf72d93bf3da7c65a9bd746 That obviously only makes sense once the changes were pushed, and especially when you can't force push onto the destination branch (which is a good idea for your masterbranch).

What does revert do in Git?

The git revert command follows this principle: it introduces a new commit to the commit history whose sole purpose is to undo the changes of a targeted commit. Importantly, this means that the existing commit history prior to the newly added “undo” commit, including the original error commit, is preserved.

How do you write a Git revision?

Other common gitrevisions are Git branch names followed by ~, followed by the number of commits behind the head commit your target commit is. In the above example, the target commit is 2 commits behind the head commit on the currently checked out branch.

What happens when you revert a commit?

When developers git revert a commit for the first time, they usually guess the command will do one of three things: The git revert will leave two files on the file system -- alpha.html and beta.html -- and will roll back to the state prior to the third commit.


1 Answers

git cherry-pick <original commit sha>
Will make a copy of the original commit, essentially re-applying the commit

Reverting the revert will do the same thing, with a messier commit message:
git revert <commit sha of the revert>

Either of these ways will allow you to git push without overwriting history, because it creates a new commit after the revert.
When typing the commit sha, you typically only need the first 5 or 6 characters:
git cherry-pick 6bfabc

like image 97
Stephan Avatar answered Oct 12 '22 00:10

Stephan