How is git revert
used?
This might sound like a duplicate question but when people ask it, the response is often, use git reset
as per Revert to a commit by a SHA hash in Git?.
Then when someone asks how to use git reset
people reply saying you should use git revert
as per Git - how to rollback.
Before you know it, eight different people appeared with their own unique ways to save the OP's ass, all of which is over your head.
So let’s try and stick the brief and write a Dummies Guide to git revert
.
A scenario: you've committed twice to master and it’s bad. You've pushed and other people have your bad changes.
You want to undo it. It’s not something you can hand-undo in code yourself, say some wizard or package manager changed tons of stuff all over the place - you just want to put it all back how it was.
This is what source control is all about. I'm sure it’s easy.
Okay, you're going to use git revert
, but how?
And after running git revert
, do you have to do something else after? Do you have to commit the changes revert made or does revert directly commit to the repository or what??
Obviously, you'll need to push again and probably announce your balls-up to the team.
git revert
simply creates a new commit that is the opposite of an existing commit.
It leaves the files in the same state as if the commit that has been reverted never existed. For example, consider the following simple example:
$ cd /tmp/example
$ git init
Initialized empty Git repository in /tmp/example/.git/
$ echo "Initial text" > README.md
$ git add README.md
$ git commit -m "initial commit"
[master (root-commit) 3f7522e] initial commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ echo "bad update" > README.md
$ git commit -am "bad update"
[master a1b9870] bad update
1 file changed, 1 insertion(+), 1 deletion(-)
In this example the commit history has two commits and the last one is a mistake. Using git revert:
$ git revert HEAD
[master 1db4eeb] Revert "bad update"
1 file changed, 1 insertion(+), 1 deletion(-)
There will be 3 commits in the log:
$ git log --oneline
1db4eeb Revert "bad update"
a1b9870 bad update
3f7522e initial commit
So there is a consistent history of what has happened, yet the files are as if the bad update never occured:
cat README.md
Initial text
It doesn't matter where in the history the commit to be reverted is (in the above example, the last commit is reverted - any commit can be reverted).
do you have to do something else after?
A git revert
is just another commit, so e.g. push to the remote so that other users can pull/fetch/merge the changes and you're done.
Do you have to commit the changes revert made or does revert directly commit to the repo?
git revert
is a commit - there are no extra steps assuming reverting a single commit is what you wanted to do.
Obviously, you'll need to push again and probably announce your balls-up to the team.
Indeed - if the remote is in an unstable state - communicating to the rest of the team that they need to pull to get the fix (the reverting commit) would be the right thing to do :).