How do I "un-revert" a reverted Git commit?
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.
If you haven't pushed that change yet,
git reset --hard HEAD^
Otherwise, reverting the revert is perfectly fine.
Another way is to
git checkout HEAD^^ -- . and then
git add -A && git commit.
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
Read more... Read less...
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 master branch). If the change has not been pushed, just do cherry-pick, revert or simply remove the revert commit as per other posts.
In our team, we have a rule to use a revert on Revert commits that were committed in the main branch, primarily to keep the history clean, so that you can see which commit reverts what:
7963f4b2a9d Revert "Revert "OD-9033 parallel reporting configuration" "This reverts commit a0e5e86d3b66cf206ae98a9c989f649eeba7965f. ... a0e5e86d3b6 Revert "OD-9055 paralel reporting configuration" This reverts commit 648d7d808bc1bca6dbf72d93bf3da7c65a9bd746. ... Merge pull request parallel_reporting_dbs to master* commit '648d7d808bc1bca6dbf72d93bf3da7c65a9bd746'
This way, you can trace the history and figure out the whole story, and even those without the knowledge of the legacy could work it out for themselves. Whereas, if you cherry-pick or rebase stuff, this valuable information is lost (unless you include it in the comment).
Obviously, if a commit reverted and re-reverted more than once that becomes quite messy.
Reverting the revert will do the trick
abcdef is your commit and
ghijkl is the commit you have when you reverted the commit
abcdef, then run:
git revert ghijkl
This will revert the revert
It is looks stupid for me. But I had been in the same situation and I did revert for reverted commits. I did number reverts so I had to do revert for each 'revert commit'.
Now my commits history looks a weird a bit.
It is a pet project so it is OK. But for real-life project I would give preference to go to last commit before reverted restore all reverted code together in one commit and more reasonable comment.
Here's how I did it:
If the branch
my_branchname was included in a merge that got reverted. And I wanted to unrevert
I first do a
git checkout -b my_new_branchname from
Then I do a
git reset --soft $COMMIT_HASH where
$COMMIT_HASH is the commit hash of the commit right before the first commit of
Then I make a new commit
git commit -m "Add back reverted changes"
Then I push up the new branch
git push origin new_branchname
Then I made a pull request for the new branch.
Or you could
git checkout -b <new-branch> and
git cherry-pick <commit> the before to the and
git rebase to drop
revert commit. send pull request like before.