In software development, reverting a commit in Git is not just a useful skill but a crucial one. Whether it's a bug introduced by a recent commit or a change that didn't work out as planned, undoing changes from a previous commit is a crucial practice for maintaining a clean and functional codebase.

This step-by-step guide will walk you through reverting a commit using Git. You'll learn how to safely undo changes without altering the commit history, ensuring your project remains stable and your version control practices remain intact. We'll cover everything from identifying the commit you need to revert to executing the command and verifying the results.

By the end of this guide, you'll be equipped with the knowledge to manage and correct your Git history confidently, feeling accomplished and confident in your abilities. You'll be able to navigate your project's history with ease, knowing that you have the skills to correct any mistakes that may arise.

What is Git Revert?

‘Git revert’ is a Git command used to undo the changes introduced by a specific commit while preserving the commit history. Unlike Git reset, which can alter the commit history and potentially rewrite past commits, Git revert creates a new commit that reverses the changes made by the targeted commit. This approach is instrumental in collaborative environments where altering commit history could impact other team members.

Key Points:

  • Non-destructive: It doesn’t remove the original commit but instead creates a new commit that effectively undoes its changes.
  • Preserves History: The original commit remains in the history, allowing you to track all changes and maintain a clear audit trail.
  • Safe for shared Repositories: Because it doesn’t rewrite history, it’s safe to use in shared repositories without affecting others' work.

Here’s a basic overview of the Git revert command:

  • Usage: git revert <commit>
  • Example: To revert a specific commit, you would use:
    • git revert <commit-hash>
  • Workflow:
    • Identify the commit you want to revert by using the Git log.
    • Use git revert followed by the commit hash to create a new commit that reverses the changes made in that commit.

This command is different from Git reset, which can alter the commit history and should be used with caution in shared repositories.

Also Read: Git Commands you should know in 2024

How Do You Revert Git Commit?

To perform a Git revert commit, you must have the ID number of the commit in question.

Steps to Follow

Here are the steps you must follow to perform a Git revert on a commit, undoing the unwanted changes:

  • Use the Git log or reflog command to find the ID of the commit you want to revert
  • Enter the Git revert command (see below), including the commit ID you want to work on
  • Provide an informative Git commit message to explain why you needed to perform the Git revert

Git Commit Message

Git commit messages explain why a software developer had to perform a Git revert commit to the team. Therefore, it’s essential to follow the seven accepted rules of standardized Git commit message writing:

  1. Obey the 50-character limit for the subject line
  2. Capitalize just the first letter in the subject line.
  3. Do not place a period at the end of the subject line.
  4. Place a blank line between the subject line and the body of the message.
  5. Limit the message body width to 72 characters.
  6. Write in the imperative.
  7. Explain what was done and why, but not how it was done.

Git Revert Command

The Git revert command syntax is straightforward. Here’s what the syntax would look like if you wanted to revert a Git commit called 31416p54:

'git revert 31416p54'

Bridge the gap between software developers and operations and develop your career in DevOps by choosing our unique Post Graduate Program in DevOps. Enroll for the PGP in collaboration with Caltech CTME Today!

How Does Git Revert Work?

Rather than removing the commit from the project history, the Git revert figures out how to invert (or reverse) the changes created by the commit, then appends a new commit containing the new inversed content. This process stops Git from losing history, vital for revising history’s integrity and promoting smooth, reliable collaboration.

The ability to Git revert a previous commit comes in handy when you’re tracking down a bug and discover that it was introduced via a single commit. Rather than going in manually, fixing the bug, and committing a new snapshot, Git revert will automatically do all the work for you.

Unlike other "undo" commands like Git checkout and Git reset, which move the branch and HEAD ref pointers to a specific commit, Git revert inverses the changes in a particular commit, creating a new "revert commit." Then, it updates the ref pointers to point at this new revert commit and makes it the branch tip.

Remember that Git revert doesn’t undo a commit in a project history; it reverses the changes made in the offending commit and appends a new commit with the opposite effect.

What does Git Revert do?

Git revert is a command used to undo changes in a Git repository by creating a new commit that reverses the changes made by a previous commit. Unlike git reset, which alters the commit history, git revert preserves the commit history by applying a new commit that effectively negates the changes from the specified commit. This is particularly useful for correcting mistakes in a collaborative environment without disrupting the project’s history.

Recommended: Everything You Need to Know About Git PUSH Command

Resetting vs. Reverting

  • The Git Revert command creates a new commit that undoes the changes made by a previous commit, preserving the commit history. It is helpful for safely undoing changes in shared repositories without altering the commit history.
  • The Git Reset command modifies the commit history by moving the current branch's head to a specified commit. It can be used to discard changes or modify the commit history, which may be risky in shared repositories as it can alter the commit history.

Git Revert Commit: An Example

We begin by creating a clean repository with the Git init command.

git@commit /c/revert example/

$ git init

Initialized empty Git repo in C:/git revert example

Now, we add five files to the repository (or “repo”). Whenever we create a new file, we add it to our Git index and create a new commit that contains a meaningful message.

git@commit /c/revert example/

$ touch alpha.html

$ git add . && git commit -m "1st git commit: 1 file"

$ touch beta.html

$ git add . && git commit -m "2nd git commit: 2 files"

$ touch charlie.html

$ git add . && git commit -m "3rd git commit: 3 files"

$ touch delta.html

$ git add . && git commit -m "4th git commit: 4 files"

$ touch edison.html

$ git add . && git commit -m "5th git commit: 5 files"

After the initial command batch, we can do a directory listing and see five files residing in the current folder.

git@commit /c/revert example/

$ ls

alpha.html  beta.html  charlie.html delta.html  edison.html

And the git reflog command reveals the history of the current commits.

git@commit /c/revert example/

$ git reflog

(HEAD -> master)

d846aa8 HEAD@{0}: commit: 5th git commit: 5 files

0c59891 HEAD@{1}: commit: 4th git commit: 4 files

4945db2 HEAD@{2}: commit: 3rd git commit: 3 files

defc4eb HEAD@{3}: commit: 2nd git commit: 2 files

2938ee3 HEAD@{4}: commit: 1st git commit: 1 file

So, we decided that the charlie.html file needs to go. That’s the third commit, with the ID of 4945db2. We enter the following Git revert command:

git@commit /c/revert example/

$ git revert 4945db2

And presto! The charlie.html is gone, as another directory listing will verify:

git@commit /c/revert example/

$ ls

alpha.html  beta.html  delta.html  edison.html

Want to Master Git? Here are the Top Git Interview Questions and Answers

How Do You Revert Commit in Git Kraken?

If you use Git Kraken, right-click on a commit from the central graph and choose Git revert commit from the context menu. You'll be asked if you want to commit the changes now. You can save your reverted commit or select no and change the Git commit message or make additional changes to the code.

Revert a Commit in Git Lens

If you use Git Lens, open your repo in VS Code, then right-click on the chosen commit in the Commits view in the sidebar to access the revert commit option.

Revert a Commit in Git Kraken CLI

If you have the free Git Kraken CLI installed, use the git revert command as follows:

git revert <commit>

The <commit> value is anything that follows the criteria of a Git revision, which Git defines. Git revisions are typically a part of the commit sha, so you’ll have to include enough of the sha so that Git Kraken CLI can identify the commit in question as unique in that particular Git repository, like so:

git revert 45111a

Explaining the Git Reset

Like Git revert, Git reset is a tool used to undo changes. Git reset has three primary invocation forms, corresponding with three command-line arguments: --soft, --mixed and –hard. The arguments, in turn, correspond to Git’s three internal state management mechanisms:

  • Commit Tree (HEAD)
  • Staging Index
  • Working Directory

Git reset moves the repository back to an earlier commit, deleting any changes made after the commit in question.

Programmers can perform a Git reset revert by typing:

git reset <file>

This action removes the named file from the staging area, and the working directory remains unchanged. However, if you want to reset the staging area so it matches the most recent commit while leaving the directory untouched, you type:

git reset

But be careful! Git reset is among the few Git commands that risk users losing their work! It can undo local changes in the Working Directory and the Staging Index.

Would You Like to Become a Full-Stack Developer?

The demand for full-stack developers is stronger than ever. Whether you're considering a career switch or looking to enhance your current developer skills, Simplilearn offers the perfect platform to help you achieve your goals.

The Post Graduate Program in DevOps program is designed to provide comprehensive knowledge of version control, containerization, and AWS. You'll gain the essential skills through immersive and hands-on training.

FAQs

1. How do I revert to a previous commit locally in Git?

Use git checkout <commit-hash> to switch to a previous commit, but this is not permanent. For a permanent change, use git revert <commit-hash> to create a new commit that undoes the changes.

2. How to return to a previous commit in Git?

Use git reset --hard <commit-hash> to return to a previous commit, discarding changes in the working directory. Be cautious, as this can overwrite uncommitted changes.

3. How do I revert a push in Git?

Use git revert <commit-hash> to create a new commit that undoes the changes, then push the new commit with git push. This approach is safe for shared repositories.

Learn from Industry Experts with free Masterclasses

  • Program Overview: Prepare for a Career as a DevOps Engineer with Caltech CTME

    DevOps

    Program Overview: Prepare for a Career as a DevOps Engineer with Caltech CTME

    27th Jun, Tuesday9:00 PM IST
  • Ignite Your DevOps Potential and Succeed in the Tech Sector

    DevOps

    Ignite Your DevOps Potential and Succeed in the Tech Sector

    3rd Apr, Wednesday7:00 PM IST
  • Career Information Session: Get Certified in DevOps with Caltech CTME

    DevOps

    Career Information Session: Get Certified in DevOps with Caltech CTME

    18th May, Thursday9:00 PM IST
prevNext