How to use Git Revert
git revert makes a new commit
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).
Closing questions
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 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 :).
Use git revert like so:
git revert <insert bad commit hash here>
git revert
creates a new commit with the changes that are rolled back. git reset
erases your git history instead of making a new commit.
The steps after are the same as any other commit.
The question is quite old but revert is still confusing people (like me)
As a beginner, after some trial and error (more errors than trials) I've got an important point:
-
git revert
requires the id of the commit you want to remove keeping it into your history -
git reset
requires the commit you want to keep, and will consequentially remove anything after that from history.
That is, if you use revert
with the first commit id, you'll find yourself into an empty directory and an additional commit in history, while with reset your directory will be.. reverted back to the initial commit and your history will get as if the last commit(s) never happened.
To be even more clear, with a log like this:
# git log --oneline
cb76ee4 wrong
01b56c6 test
2e407ce first commit
Using git revert cb76ee4
will by default bring your files back to 01b56c6 and will add a further commit to your history:
8d4406b Revert "wrong"
cb76ee4 wrong
01b56c6 test
2e407ce first commit
git reset 01b56c6
will instead bring your files back to 01b56c6 and will clean up any other commit after that from your history :
01b56c6 test
2e407ce first commit
I know these are "the basics" but it was quite confusing for me, by running revert
on first id ('first commit') I was expecting to find my initial files, it taken a while to understand, that if you need your files back as 'first commit' you need to use the next id.
The reason reset
and revert
tend to come up a lot in the same conversations is because different version control systems use them to mean different things.
In particular, people who are used to SVN or P4 who want to throw away uncommitted changes to a file will often reach for revert
before being told that they actually want reset
.
Similarly, the revert
equivalent in other VCSes is often called rollback
or something similar - but "rollback" can also mean "I want to completely discard the last few commits", which is appropriate for reset
but not revert
. So, there's a lot of confusion where people know what they want to do, but aren't clear on which command they should be using for it.
As for your actual questions about revert...
Okay, you're going to use git revert but how?
git revert first-bad-commit^..last-bad-commit
EDIT: note the ^
character for the first-bad-commit. This is referencing the parent of first-bad-commit
, because the revert range does NOT include the starting commit.
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 repo or what??
By default, git revert
prompts you for a commit message and then commits the results. This can be overridden. I quote the man page:
--edit
With this option, git revert will let you edit the commit message prior to committing the revert. This is the default if you run the command from a terminal.
--no-commit
Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.
This is useful when reverting more than one commits' effect to your index in a row.
In particular, by default it creates a new commit for each commit you're reverting. You can use revert --no-commit
to create changes reverting all of them without committing those changes as individual commits, then commit at your leisure.