How to Revert the Latest Commit in Git — A Beginner-Friendly Guide
Anusha Kuppili

Anusha Kuppili @anusha_kuppili

About: 👩‍💻 DevOps & Cloud Enthusiast | Passionate about Containerization, MLOps & Full-Stack Projects | Sharing What I Learn | Creator @ Data Enthusiast Era 🎥

Location:
India
Joined:
Jul 7, 2025

How to Revert the Latest Commit in Git — A Beginner-Friendly Guide

Publish Date: Jul 28
0 0

Hello Dev Community! 👋

Today, I’m going to walk you through a simple and practical Git task that every developer or DevOps engineer should know — reverting the latest commit in a Git repository.

Imagine you’re working with a team, and the latest commit caused some problems. What do you do next? Revert that commit and get your repository back to a stable state.

Let’s dive in with a real-world example from the Nautilus application development team!


Scenario

The Nautilus team has a Git repository located at:

/usr/src/kodekloudrepos/media
Enter fullscreen mode Exit fullscreen mode

This repo is on a storage server in their data center. They noticed the latest commit caused issues, and they want to revert that commit and return the repo to the previous stable commit — in this case, the initial commit.


Step 1: Navigate to Your Repository

Start by opening your terminal and moving into your Git repo directory:

cd /usr/src/kodekloudrepos/media

Enter fullscreen mode Exit fullscreen mode

Make sure you have the right permissions and access to the repo on the server.


Step 2: Inspect the Commit History

Before making any changes, it’s a good idea to review the commit history to confirm which commit you want to revert to.

Run:

git log --oneline

Enter fullscreen mode Exit fullscreen mode

This command shows a concise list of commit hashes and their messages. Look for the latest commit at the top and the one with the message initial commit below it — that’s the safe state you want to revert to.


Step 3: Revert the Latest Commit

Now, to undo the most recent commit without losing the rest of your commit history, you can use:

git revert HEAD

Enter fullscreen mode Exit fullscreen mode

This command tells Git to create a new commit that undoes the changes introduced by the last commit.

Important: After running this, Git will open your default editor to edit the commit message. By default, it looks like this:

Revert "Commit message of latest commit"

This reverts commit .

Delete the default text and replace it entirely with:

revert media

Enter fullscreen mode Exit fullscreen mode

Make sure it's all in lowercase, as per the requirement.

To save and exit:

  • In Nano editor: Press Ctrl + X, then Y, then Enter.
  • In Vim editor: Press Esc, type :wq and press Enter.

Step 4: Verify The Revert

To confirm that the revert commit has been made:

git log --oneline

Enter fullscreen mode Exit fullscreen mode

You should now see a new commit at the top with the message revert media. This means the repository has effectively been rolled back to the previous commit state.


Quick Recap

  • Navigate to the repo: cd /usr/src/kodekloudrepos/media
  • Check commits: git log --oneline
  • Revert the last commit: git revert HEAD and edit commit message to revert media
  • Verify with git log --oneline

Why Use git revert Instead of git reset?

git revert is a safe choice in shared repositories because it creates a new commit that undoes the previous one, preserving history and ensuring that collaborators won't face complications.

git reset rewrites history and can cause issues if others have already pulled the commits you want to remove.


Final Thoughts

Reverting commits is a handy skill to keep your project clean and stable, especially when things don’t go as planned. Git makes it quite straightforward, even for beginners.

If you found this tutorial useful, feel free to leave a comment or share your own Git tips!

Happy Coding! 🚀


Comments 0 total

    Add comment