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

Git Add All Modified Files

When you are dealing with Git add, you have multiple options to add all modified files. Let’s look at a few scenarios...
28/12/2020

Configure Git Server with HTTP on Ubuntu

If you want to setup a Git HTTP Server for working with Git repositories privately, then this article is for you.  In...
29/12/2020

Git Branch Basics

Basics of Git Branching The ability to easily branch is one of the best features of Git. Creating branches in other version...
28/12/2020
Bài Viết

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

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

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024