We are going to start with adding a few files. On the last commit, we are going to both add and delete files to create a messy situation. Then we are going to revert back to the state before the chaos.
You can create a folder called /test and run the following commands to initialize Git and create the situation described above (We are intentionally doing separate commits to create a history):
Initialized empty Git repository in /Users/zakh_eecs/_work/LearnGIT/git_revert/test/.git/
$ echo x > file_1.txt
$ git add -A
$ git commit -m "Adding file_1.txt"
[master (root-commit) 08caf5d] Adding file_1.txt
1 file changed, 1 insertion(+)
create mode 100644 file_1.txt
$ echo y > file_2.txt
$ git add -A
$ git commit -m "Adding file_2.txt"
[master ba18a2f] Adding file_2.txt
1 file changed, 1 insertion(+)
create mode 100644 file_2.txt
$ echo z > file_3.txt
$ git add -A
$ git commit -m "Adding file_3.txt"
[master 97f09ad] Adding file_3.txt
1 file changed, 1 insertion(+)
create mode 100644 file_3.txt
$ echo u > file_4.txt
$ git add -A
$ git commit -m "Adding file_4.txt"
[master 9caf084] Adding file_4.txt
1 file changed, 1 insertion(+)
create mode 100644 file_4.txt
$ echo v > file_5.txt
$ git add -A
$ git commit -m "Adding file_5.txt"
[master 3f228b2] Adding file_5.txt
1 file changed, 1 insertion(+)
create mode 100644 file_5.txt
If we check our folder, we should see the following situation:
file_1.txt
file_2.txt
file_3.txt
file_4.txt
file_5.txt
If we check the history, we should have the following files:
3f228b2 Adding file_5.txt
9caf084 Adding file_4.txt
97f09ad Adding file_3.txt
ba18a2f Adding file_2.txt
08caf5d Adding file_1.txt
Now let’s create some havoc, we are going to delete a few files and add a bad file.
$ rm file_4.txt
$ echo w > my_bad_file.txt
$ git add -A
$ git commit -m "Added and deleted files without thinking of consequences"
[master 879fbf8] Added and deleted files without thinking of consequences
3 files changed, 1 insertion(+), 2 deletions(-)
delete mode 100644 file_2.txt
delete mode 100644 file_4.txt
create mode 100644 my_bad_file.txt
Now, this is the condition of our folder:
file_1.txt
file_3.txt
file_5.txt
my_bad_file.txt
And this the condition of our history:
879fbf8 Added and deleted files without thinking of consequences
3f228b2 Adding file_5.txt
9caf084 Adding file_4.txt
97f09ad Adding file_3.txt
ba18a2f Adding file_2.txt
08caf5d Adding file_1.txt
We realize that we don’t want the last commit 879fbf8. So we use the following the revert command:
It will open up a text window for editing the automatic comment:
This reverts commit 879fbf849c4bd6fb9a377604d6355c76b92a832c.
# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# new file: file_2.txt
# new file: file_4.txt
# deleted: my_bad_file.txt
#
You can modify the comment. We are going to keep it as it is. As soon as you save the comment window, the revert task will take place:
[master 6e80f0e] Revert "Added and deleted files without thinking of consequences"
3 files changed, 2 insertions(+), 1 deletion(-)
create mode 100644 file_2.txt
create mode 100644 file_4.txt
delete mode 100644 my_bad_file.txt
Let’s look at our folder now:
file_1.txt
file_2.txt
file_3.txt
file_4.txt
file_5.txt
Our files are back in sequence like before. All the additions and deletions have been reverted. Let’s check the log:
6e80f0e Revert "Added and deleted files without thinking of consequences"
879fbf8 Added and deleted files without thinking of consequences
3f228b2 Adding file_5.txt
9caf084 Adding file_4.txt
97f09ad Adding file_3.txt
ba18a2f Adding file_2.txt
08caf5d Adding file_1.txt
There is a new commit 6e80f0e. Any changes that were part of 879fbf8 was undone and then committed in 6e80f0e.
Warning: Git reset command allows you to undo commits also. But in the reset case (especially hard reset), it would have deleted the 879fbf8 commit like it never happened and there would have been no 6e80f0e commit. With a revert command, everyone can see the changes that have taken place. In the reset case, no trace is left. So it’s a bad idea to use reset command in a public repository because it can cause mass confusion. The golden rule is – don’t use reset in public repositories, use revert which is safer.
In Conclusion:
The Git revert command is a fast and convenient way to remedy your mistakes. It’s a command you should remember if you working regularly with Git.