๐Ÿ“˜Git and Github
Estimated reading: 3 minutes 344 views

Git Revert: A Safe Way to Undo Changes in Git Repositories


What is git revert?

git revert is a Git command that safely undoes changes made by a specific commit by creating a new commit that negates the effect of the selected one.

Unlike git reset, it does not rewrite history, making it ideal for shared repositories and team workflows.


Key Characteristics of git revert

Feature Description
Non-destructiveCreates a new commit instead of deleting or modifying past ones
Collaboration-safeWorks perfectly in shared repos without history conflicts
Targeted UndoYou can revert one or more specific commits
AuditableKeeps a traceable record of all changes, including reverts

Basic Syntax

git revert <commit>

Replace <commit> with the commit hash (from git log) or use HEAD to target the most recent commit.


Common Use Cases with Examples


1. Revert the Most Recent Commit

git revert HEAD

This creates a new commit that undoes the last commit in the current branch.


2. Revert a Specific Commit by Hash

git revert abc1234

This undoes the changes introduced in the commit with hash abc1234.


3. Revert a Range of Commits

git revert oldest-commit^..newest-commit

Useful when multiple consecutive commits introduced a problem. The ^ includes the starting commit.


4. Revert a Merge Commit

git revert -m 1 merge-commit-hash

The -m flag specifies which parent is considered the โ€œmainlineโ€ (usually 1 = main branch).


Common Options

OptionDescription
--no-editUse the default commit message generated by Git
-n / --no-commitStage the changes from the revert but donโ€™t commit automatically
-m <number>Required for reverting a merge; specifies the parent branch to follow

Example Workflow: Reverting a Bug

Step 1: Discover the buggy commit

git log --oneline
# Output:
# abc1234 Added buggy login logic

Step 2: Revert it safely

git revert abc1234

Git opens a text editor with a default revert message.

Step 3: Save and exit to create the new commit

Revert "Added buggy login logic"

Comparison: git revert vs git reset

Featuregit revertgit reset
Alters commit history No Yes
Safe for shared repos Yes No
Creates new commit Yes No
Can target old commits Yes Yes
Keeps commit log trace Yes No

Best Practices for Using git revert

  1. Use revert for commits that are already pushed to a shared or public repository.
  2. Always review commit messages before confirming reverts.
  3. For merge commits, be sure to use -m to indicate the mainline parent.
  4. Use --no-commit if you want to revert and make adjustments before finalizing the commit.
  5. Avoid using reset on public branches โ€” it rewrites history and can confuse collaborators.

Summary โ€“ Git Revert

git revert is a team-friendly and non-destructive method for undoing Git changes by creating a new commit that undoes a prior one.

Key Takeaways:

  • It preserves history and is safe to use on shared branches
  • You can revert single or multiple commits
  • It is perfect for collaborative teams and production environments

Real-World Relevance:

Whether you’re undoing a faulty deployment, cleaning up experimental features, or rolling back bugs, git revert ensures clarity, traceability, and safety in your workflow.


Git Revert โ€“ FAQ


Can I revert multiple commits at once?

Yes. Use:

git revert abc1234 def5678

Or a range:

git revert abc1234^..def5678

What does -m mean in merge reverts?

-m (mainline) tells Git which parent branch to treat as the main line. Example:

git revert -m 1 <merge-commit>

Can I cancel a revert?

Yes, by reverting the revert:

git revert <revert-commit-hash>

Does git revert delete the original commit?

No. It only reverses the changes while keeping the original commit in history.


How do I find the commit hash?

Run:

git log --oneline

Copy the short hash (e.g., abc1234) from the list.


Share Now :
Share

Git Revert

Or Copy Link

CONTENTS
Scroll to Top