Git

Git How to Revert Last Commit?

Git How to Revert Last Commit?
Git is an effective tool that keeps track of your code. And one of the great features of the tool is that you can easily check your history and revert back if you have made a mistake. Let's create a situation where you can revert the last commit and see how the Git revert command works.

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):

$ git init
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:

$ ls -1
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:

$ git log --oneline
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_2.txt
$ 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:

$ ls -1
file_1.txt
file_3.txt
file_5.txt
my_bad_file.txt

And this the condition of our history:

$ git log --oneline
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:

$ git revert 879fbf8

It will open up a text window for editing the automatic comment:

Revert "Added and deleted files without thinking of consequences"
 
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:

$ git revert 879fbf8
[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:

$ ls -1
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:

$ git log --oneline
 
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.

Further Study:

OpenTTD vs Simutrans
Creating your own transport simulation can be fun, relaxing and extremely enticing. That's why you need to make sure that you try out as many games as...
OpenTTD Tutorial
OpenTTD is one of the most popular business simulation games out there. In this game, you need to create a wonderful transportation business. However,...
SuperTuxKart for Linux
SuperTuxKart is a great title designed to bring you the Mario Kart experience free of charge on your Linux system. It is pretty challenging and fun to...