Git cancel a revert
GitVersion ControlGit Problem Overview
In git let say I commit A and B
A---[B]
But then I revert with
git revert HEAD
So I am there now:
[A]---B
How do I cancel my revert so that I can go back to B?
Git Solutions
Solution 1 - Git
You have two general choices:
- Revert the revert commit (creating a second revert commit that takes you back to the original)
- Throw away the revert commit with
> git reset --hard HEAD^
The second option is only appropriate if you have not pushed your changes anywhere else. In fact, if you haven't pushed your first revert commit anywhere yet, you can simply use
> git reset --hard
to roll back without creating any revert commits at all.
Solution 2 - Git
If you haven't done it completely, i.e., in gitbash
you see something like:
Username@Host MINGW64 /d/code/your-project (feature|REVERTING)
then you can use git revert --abort
to abort.
If you have done it.. just don't reset, the changes are still there. Use git reset
to change the state. Instead of --hard
, you can also use --soft
(keep all the changes).
git reset --soft HEAD^ // discard the last commit, keeping all the changes after that
Also, if you want to revert more than 1 commits:
git reset --soft HEAD~3 // discart last 3 commits. Don't know if it works for commits of others.
Solution 3 - Git
As you create a new commit
which reverts another commit
you can treat it like a normal commit.
So basically you have many choices, such as
> - git rebase -i
(remove the revert commit)
> - git reset --hard <commitID>
(reset to the commit before the revert, you'll lose all local changes)
> - git reset --soft <commitID>
(same as above but keeps local changes)
> - technically you can use git revert <commitId>
to revert your revert