Git: How to Revert Last Commit?

28/12/2020
Git
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:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Make a Pull Request in Github

Git is a Version Control System (VCS). GitHub is a collaboration tool built on top of Git. It is also a Git graphical frontend....
29/12/2020

How to Install and Configure GitLab on Debian 9

Recently Microsoft bought GitHub. Those of you Microsoft haters may say GitHub is not trustable anymore. Well, I won’t...
29/12/2020

How to Cherry Pick in Git

git cherry pick is a merge feature of Git. But there is a slight difference in git cherry pick and git merge. Also, the...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024