Knowing how to revert a commit in Git is a vital skill for maintaining code integrity. Whether you’ve introduced a bug, pushed an unintended change, or simply need to backtrack, the ability to safely undo changes is essential for keeping your codebase clean, stable, and production-ready.

The Git revert a commit step-by-step guide will show you how to revert a commit in Git without rewriting history. From identifying the right commit to executing the command and resolving any conflicts, you’ll understand the entire process deeply.

How Does Git Revert Command Work?

The revert command in Git creates a new commit that undoes the changes made by a previous commit. Here’s how it works:

  • Identifies the commit
  • Creates a new commit
  • Safeguards history
  • Resolves conflicts
  • Pushes the reverted commit
Also Read: Git Commands you must know

How to Revert a Commit in Git?

The git revert command allows you to undo a specific commit without rewriting the entire project history.

Syntax:

git revert <commit_hash>

This command creates a new commit that undoes the changes introduced by the specified commit — without altering the project’s history.

Here’s the step-by-step guide to revert a commit:

1. Find the Commit Hash

Use the following command to view your Git history:

git log

This will list all previous commits along with their commit hashes.

commit 9fceb02... (HEAD -> main)
Author: Jane Doe
Date:   Mon Apr 8 10:15:32 2025 +0530
Added login validation

Copy the commit hash of the commit you want to revert.

2. Run the Revert Command

Now, revert the commit using the copied hash:

git revert 9fceb02

This will open the configured editor to confirm or edit the default commit message. Save and close it to proceed.

3. Resolve Merge Conflicts

If the reverted commit conflicts with other changes in the branch, Git will pause the process and ask you to resolve the conflicts manually. Once resolved:

git add .
git revert --continue

Example 1: Faulty CSS Update

You can undo a faulty-committed CSS file update with,

git log
# Copy the hash: a1b2c3d
git revert a1b2c3d
# Edit message if needed and save
git push origin main
Join Simplilearn's Full Stack Java Developer Masters Program and learn everything from Java fundamentals to advanced frameworks. Equip yourself with the tools and knowledge to excel in today’s tech-driven world.

How to Revert a Commit in Git After Push?

The revert a commit in Git after Push creates a new commit that undoes the changes of the previous commit without altering the commit history. This is the best and safest way to undo a commit in a shared repository because it avoids rewriting history.

Here’s the step-by-step guide to revert a Commit after Push:

1. Check the Commit History

Use git log to identify the commit you want to undo:

git log

Look for the hash of the commit you want to revert (example: 3e1f1b2).

2. Run the Revert Command

Use the commit hash with git revert:

git revert 3e1f1b2

This will open the editor to confirm the default commit message. Save and close the editor to complete the revert.

3. Push the Revert Commit

After the revert is committed locally, push it to the remote repository:

git push origin main

This adds a new commit that undoes the changes without rewriting history.

How to Revert Multiple Commits in Git?

Git allows you to revert multiple commits without rewriting history. This is especially useful when you’ve already pushed the changes or are working collaboratively.

Syntax:

git revert <oldest_commit_hash>^..<newest_commit_hash>
  • The caret (^) includes the oldest commit in the range
  • Git will revert each commit individually, creating a new one for each

Example 2: Revert a Feature Rollout

The commit history looks like the following:

a1b2c3d - Added feature A (latest)

f6g7h8i - Updated tests

j1k2l3m - Initial setup for feature A

To revert all three commits related to feature A, run:

git revert j1k2l3m^..a1b2c3d

Git will create three new revert commits, one for each original commit.

Git Revert Command Summary

Here’s a table summarizing the various Git Revert Commands:

Action

Command

Description

View commit history

git log

Shows commit hashes and messages

Revert a single commit

git revert <commit_hash>

Creates a new commit that undoes the selected commit

Revert multiple commits (range)

git revert <oldest_hash>^..<newest_hash>

Reverts all commits in the specified range

Revert a merge commit

git revert -m 1 <merge_commit_hash>

Reverts a merge (use -m 1 to specify mainline)

Push changes after revert

git push origin <branch_name>

Pushes the new revert commit to remote repo

Resolve conflicts (if any)

git add . → git revert --continue

Handles conflicts during revert

Conclusion

Reverting a commit in Git is a powerful technique to undo changes while maintaining a decent commit history. By using the git revert command, you can revert unwanted changes without disrupting the workflow of other collaborators. This approach allows you to rectify errors in a structured manner, ensuring the project is stable and consistent.

Are you looking to deepen your development or deployment expertise? Explore the Full Stack Java Developer Master's Program to gain hands-on experience with Git, Java, Spring Boot, and more. Or, if you're aiming to scale your DevOps and cloud infrastructure skills, the Cloud Computing and DevOps Certification Program is designed to help you master Git workflows, CI/CD pipelines, and cloud platforms like AWS and Azure.

Upskill now and unlock new career opportunities in tech!

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

  • Key 2025 Software Development Trends- Learn How To Leverage them for your career

    Software Development

    Key 2025 Software Development Trends- Learn How To Leverage them for your career

    9th Dec, Monday9:30 PM IST
  • Must-Know Full Stack Java Dev Career Trends for 2024

    Software Development

    Must-Know Full Stack Java Dev Career Trends for 2024

    6th Aug, Tuesday9:00 PM IST
  • Full Stack Java Development: A 2024 Blueprint for Recession-Proofing Your Career

    Software Development

    Full Stack Java Development: A 2024 Blueprint for Recession-Proofing Your Career

    27th Jun, Thursday7:30 PM IST
prevNext