How to Undo Last Commit: Git Revert Commit


Do you want to develop software? Do the constant changes in the source code bother you and your team? Git can resolve this issue. Yes, Git is a distributed version control system that tracks changes in source code. 

This article will take you through the core concept of Git. We will discuss all the relevant terms associated with this powerful tool, such as repositories, branches, commit, and more. We will guide you to Undo Last Commit in Git and teach you about tracking changes. Let’s start by exploring the concept of Git tool. 

Explain Git in Detail: List Its Basic Elements

Explain Git in Detail: List Its Basic Elements

This fantastic tool, Git, allows multiple developers to collaborate on a single project. It takes snapshots of every change made by the collaborator and contains metadata such as author and timestamp. Let’s take a look at the basic elements of the Git tool, after which we will talk about the Git undo commit.

  • Commit: These are the changes made to the source code or file.
  • Repository or repo: This is like a project folder that contains the software. 
  • Branches: This is similar to a duplicate document. The Git tool assigns the duplicate document as a branch. 
  • Merge: Once the changes or “commits” are done, the local copy is combined with the main project. This is called merge. 
  • Pull and push: These are the two frequently used elements in Git. The pull option relates to getting the changes from the Git repo to the local copy. On the other hand, the push option drops the changes or “commits” back to the main project. 

How to Revert Commit?

How to Revert Commit?

The revert commit or Git undo commit is related to taking a step back regarding the change. This process is slightly different as it keeps the record or changes intact even after the revert is done. Moreover, each commit is assigned a unique identifier, which is called a “hash” or “commit ID.” Let’s see how to revert the commit in Git

  1. Identify the commit to revert: Encircle the hash or identifier of the commit you want to revert. Simply go to the Git history viewer to locate the desired hash.
  2. Open your terminal or command prompt: Go to the Git repository through the command line. Use the following syntax in case the directory is placed on the desktop:
cd ~/Desktop/my-git-repo
How to Revert Commit?
  1. Initiate the revert: Use the git revert command followed by the commit hash of the commit you want to revert. For example:
git revert <commit-hash> 
  1. Resolve any conflicts (if necessary): In case there is a conflict during the Git undo commit, you will have to resolve it manually. Always use “Git status” to look for any conflicts.
  2. Commit the reversion: Any resolution to the conflict will be met with a default message from Git. This message will have the information about the revert. All you have to do is save and close the editor to complete the process of reverting commit from Git.

The above steps will undo the last commit in Git. Now, let’s focus on the reset option.

Git Reset: Revert Unpublished Commits

Git Reset: Revert Unpublished Commits

Unpublished commits are those changes that are still with the collaborator and not shared with others. These commits stay in the local repository as compared to others that become a part of the remote repository. Learn how to revert unpublished commits through the following steps:

  • Identify the commit that you want to reset. You can use the commit ID to do so.
  • Open your terminal and go to the directory of your Git repository.
  1. Reset the branch to the desired commit: Use the git reset command in the following manner: 
git reset --mixed <commit-hash> 

In the above syntax, replace the “<commit-hash>” with the correct commit ID.

  • Once the reset is done, review the changes in your current directory using git status. 
  • If the unpublished changes are perfectly reverted in your working directory, you can commit them as usual using git commit.
git commit -m "Revert unpublished commits" 
  • The last step is to force-push your changes to update the remote branch. You can use the following command to do so:
git push origin <branch-name> --force

Here, replace the “<branch-name>” with the name of your branch.

Git Reset: Revert Unpublished Commits

Git Amend: Modify the Last Commit

Git Amend: Modify the Last Commit

Modifying (using git commit—-amend) and undoing/reverting (using git revert or other methods) are two different processes. When you modify a commit, Git allows you to make changes to the most recent commit without creating a new one. Small adjustments are mostly considered modifications or amendments while undoing or reverting a commit involves creating a new commit. The following command will easily modify the last commit:

git commit --amend
Git Reset: Revert Unpublished Commits

Reset Vs Revert: Which One is More Suitable?

It is important to know the exact purpose of each option. You can use Git reset to discard changes introduced by one or more commits. The reset option is used to implant the branch into a previous state. On the other hand, git revert is a safer option for undoing commits in Git on shared branches that are already pushed to a remote repository. This option creates a new commit to undo particular changes while safely preserving the commit history. 

Also Read: How Do I Run and Install Git on Windows? – Complete Guide


The version control system or Git is a great tool for managing commits in a clean and organized manner. This article explores two fundamental methods for managing commits: reverting and resetting. Reverting commits in Git provides a safe way to undo changes while preserving the commit history. This creates a new commit to undo specific changes, ensuring that the original changes are still documented. Resetting commits in Git, on the other hand, is useful for discarding changes introduced by one or more commits. It allows you to reset the branch to a previous state, effectively removing commits from history. However, it’s essential to exercise caution when using reset, especially on shared branches, as it can alter the commit history for all collaborators. We hope this informative piece has led to a better understanding of Git undo commit.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

[sc name="footer"][/sc]