What Is Git Revert? – ITU Online IT Training

What Is Git Revert?

Ready to start learning? Individual Plans →Team Plans →

What Is Git Revert? A Practical Guide to Undoing Commits Safely

If you need to cancel git revert-style changes in a shared repository, the first thing to understand is that Git usually does not delete anything. git revert creates a new commit that undoes the changes from an earlier commit, which is exactly why teams use it on branches that other developers already pulled.

That distinction matters. When a bad change reaches main, release, or any shared integration branch, the goal is usually to remove the effect of the change without breaking everyone else’s local history. This article explains what git revert does, how it works, when to use it, when not to use it, and how it differs from commands like git reset.

You will also see how to find the right commit, resolve conflicts, and revert one commit or a sequence of commits without creating more damage. The core theme is simple: safe collaboration depends on preserving history, not rewriting it blindly.

Git revert is a recovery tool, not a history eraser. It keeps the record intact and adds a new commit that applies the opposite of the selected change.

Key Takeaway

If the commit is already shared with other developers, git revert is usually the safer choice. It backs out the change without forcing everyone to re-sync rewritten history.

Understanding Git Revert

git revert is the Git command you use when you want to undo the effect of a specific commit while keeping the repository history intact. In plain English, it is the inverse of a commit: Git calculates what that commit changed and creates a new commit that reverses those changes.

This fits Git’s role as a version control system perfectly. Git is designed to preserve the evolution of a codebase so teams can inspect what changed, when it changed, and who changed it. That history is useful for debugging, audits, incident response, and code review. If a regression appears in production, you want to know exactly which change introduced it and exactly how it was backed out.

People often use “undo” to mean “delete,” but those are not the same thing in Git. A revert does not remove the original commit from the log. It leaves the original commit in place and adds a new commit on top. That makes delete git commit and delete commit from history the wrong mental model for this command.

This is especially useful in collaborative environments where multiple people work from the same branch. If the commit is already published, rewriting history can disrupt teammates, invalidate their local work, and create avoidable merge problems. git revert avoids that by changing the code, not the timeline.

  • Use revert for shared work: safer on public branches.
  • Preserve visibility: the original change remains traceable.
  • Support audits and debugging: both the mistake and the fix are recorded.

For official Git documentation on commit history and command behavior, the best reference is the Git documentation. If you want a broader view of why traceability matters in secure development workflows, the NIST Secure Software Development Framework is also worth reading.

How Git Revert Works Under the Hood

Under the hood, git revert does something very specific: it computes the diff introduced by the target commit, then applies the opposite diff as a new commit. That means the original commit stays exactly where it is in the graph. Git does not edit the commit object or change its hash. Instead, it builds a new commit with a new hash that reverses the earlier change.

This is why revert is so traceable. The commit graph remains intact, and anyone reviewing the history can see the original change, the revert, and any later follow-up fixes. That matters in teams that need to explain why a feature disappeared, why a bug fix was rolled back, or why a release branch diverged from development for a period of time.

What happens when files changed again after the target commit?

This is where things can get interesting. If the target commit touched a file that has since been modified by other commits, Git may not be able to apply the inverse cleanly. In that case, you get a conflict. The conflict does not mean revert failed completely; it means Git needs help deciding what the final code should look like after all changes are considered.

For example, imagine a commit changed a validation function last week, but three other commits have since updated the same function for a different feature. Reverting the old commit may overlap with new edits in the same lines. Git will mark the conflict, and you will need to inspect the file manually.

Why the revert commit becomes part of history

Because Git stores history as a chain of commits, a revert is just another commit in that chain. That is a good thing. It creates an audit trail: “this was changed,” then “this was undone,” then possibly “this was revised again.” In regulated environments and enterprise teams, that level of transparency is often more valuable than a perfectly clean but rewritten timeline.

Note

Git uses content snapshots and parent references to build its history. A revert preserves those references, which is why it is easier to trace than a reset on a shared branch.

For a deeper look at Git internals and command behavior, see the official git revert documentation. If you work in environments that emphasize software provenance and change control, the NIST Software Assurance program provides useful context.

Why Git Revert Is Safer Than Rewriting History

Rewriting history in a collaborative Git repository is risky because every clone of that repository may already have the old commits. If you change published commit IDs with commands like git reset followed by a force push, other developers can end up with diverged histories, confusing merge messages, or lost local work.

git revert avoids that problem. It does not alter the public branch history. Teammates who already pulled the branch will simply receive a new commit that cleanly backs out the earlier change. Their local branch still makes sense, and the history stays aligned across the team.

This matters most on shared branches such as main, master, release, and hotfix branches. Those branches are often tied to CI pipelines, deployment automation, pull request review, and release tagging. If you rewrite them carelessly, you can break build references, invalidate deployment approvals, or make incident review harder than it needs to be.

Git Revert History Rewriting
Preserves existing commits Changes commit history and hashes
Safe for shared branches Risky when others have pulled the branch
Creates a clear audit trail Can obscure what really happened
Usually easier for teams to follow Can require coordination and force pushes

The safer approach also reduces confusion during incidents. If production is failing and a rollback is needed, the team can see exactly which commit was reversed and why. That kind of clarity helps during postmortems, release reviews, and incident response.

For broader secure-development guidance, the OWASP Software Assurance Maturity Model and NIST SSDF both reinforce controlled, traceable change management.

Benefits of Using Git Revert

The biggest benefit of git revert is simple: it lets you undo a change without creating a mess for everyone else. That is valuable in active repositories where multiple developers are committing every day. Instead of deleting history, you add a corrective commit that makes the project stable again.

That stability helps in several concrete ways. It preserves the full record of what happened, which is useful during code reviews and root-cause analysis. It also makes it easier to spot patterns, such as a feature that keeps getting reverted because it was merged too early or tested too lightly. Over time, that history becomes evidence for better engineering decisions.

Practical benefits teams actually feel

  • Cleaner collaboration: teammates do not need to rebase around rewritten history.
  • Better debugging: you can compare the original commit and its revert to understand the failure.
  • Lower operational risk: you can roll back only the bad change, not unrelated work.
  • Stronger accountability: the timeline shows both the mistake and the response.
  • Faster incident response: teams can act quickly without debating whether history should be rewritten.

This is also why revert shows up frequently in release management. If a patch introduces a regression, reverting just that patch may be enough to stabilize the branch while the team investigates the deeper issue. You do not always need a large rollback. Sometimes you only need to cancel the effect of one commit and move on.

That said, revert is not magic. It works best when you combine it with testing, code review, and clear communication. A revert commit that goes in untested can still fail in new ways if the codebase has moved on since the original change.

For team-based change management guidance, the Git revert reference is a useful plain-language explanation, and the CISA secure software development resources provide a good security-focused angle.

When to Use Git Revert

Use git revert when you need to undo a change that is already part of a shared history and you want to keep that history intact. That is the most common answer to the question, “How do I cancel git revert-style damage safely in a team repo?”

A classic example is a bug that reaches production. Suppose a commit changes a payment flow and causes checkout failures. If the change is already merged and deployed, reverting it may be the fastest way to restore service while the team works on a proper fix. In that case, the priority is reducing impact, not cleaning up the log.

Good use cases for revert

  • Shared branch rollback: remove a bad commit from main or release.
  • Production regression: back out a change that introduced errors or broken behavior.
  • Temporary feature removal: disable a feature while keeping the code history visible.
  • Problematic merge recovery: undo a merge that brought in unwanted changes.
  • Release stabilization: remove one bad change without undoing the whole release train.

Revert is also a strong choice when traceability matters more than a pristine-looking branch. Auditors, senior engineers, and incident responders often want to know exactly what happened and exactly how the problem was corrected. Revert gives them that record.

For teams working in secure or regulated environments, traceable rollback aligns well with formal change management expectations. The ISO/IEC 27001 framework and NIST guidance both emphasize controlled changes, accountability, and documented responses.

Pro Tip

If the bad commit has already been deployed or reviewed by several people, revert first and refactor later. Stabilize the branch before you make the fix elegant.

When Not to Use Git Revert

There are times when git revert is the wrong tool. If the commit is private and has not been pushed to a shared remote, a simpler history rewrite may be cleaner. For example, if you made a mistake in your own local branch and no one else has seen it, git reset may remove the work more directly.

That difference matters because revert adds a new commit. If you are still experimenting and repeatedly undoing and redoing the same change, your history can become noisy. In that case, you may want to clean up locally before publishing anything. A tidy local branch is easier to understand than a public branch with a long trail of reversions and re-reversions.

Situations where revert is not ideal

  • Unpublished local work: reset is often better when nobody else has pulled the commits.
  • Repeated flip-flopping: constant revert-and-reapply cycles add clutter.
  • Large architectural mistakes: a revert may only treat the symptom, not the root cause.
  • Complex dependency chains: reversing one commit may break later commits that depend on it.
  • History cleanup needs: sometimes you need a branch rebuild, not a revert.

Do not use revert as a substitute for design correction. If the real problem is that the feature was built on the wrong abstraction, reverting only the last commit may not fix the underlying architecture. You may need a larger code change, a new branch strategy, or a deeper redesign.

For private work, the official git reset documentation is the right reference. For collaboration guidance, the practical rule is simple: reset locally, revert publicly.

How to Revert a Commit Step by Step

Reverting a commit is straightforward once you know the hash. Start by finding the commit you want to undo, confirm that it is the right one, and then run the revert command. The general pattern is git revert <commit-hash>.

  1. Find the commit. Use git log --oneline to inspect recent history.
  2. Verify the target. Check the message, timestamp, and affected files.
  3. Run the revert. Use git revert <commit-hash>.
  4. Resolve conflicts if needed. Edit the files Git flags as conflicting.
  5. Finalize the commit. Git will create the revert commit after you save the resolution.
  6. Push the change. If the branch is remote, push the revert like any other commit.

A common example might look like this:

git log --oneline
git revert a1b2c3d
git status
git push origin main

If Git opens an editor for the commit message, do not ignore it. The message should clearly state what was reverted and, ideally, why. A good revert message helps your teammates understand whether the rollback was due to a bug, a regression, or a failed experiment.

If the commit is a merge commit, you may need to specify the parent using the -m option. That is a more advanced case, but it comes up often enough in branch-heavy workflows that teams should understand it before they need it.

For command behavior, the Git revert manual remains the authoritative source.

How to Find the Right Commit to Revert

Choosing the correct commit is where many teams slow down, and for good reason. If you revert the wrong commit, you can remove a valid fix, create a second bug, or end up chasing your own tail with follow-up reverts. The goal is not just to undo something. The goal is to undo the right thing.

Start with git log. Read the commit messages, look at the timestamps, and check who authored the change. Then inspect the files touched by the commit. If the commit message says “fix search filtering” but the affected files are in authentication, you should slow down and investigate further.

Ways to validate the target commit

  • Review commit messages: they often explain intent better than code alone.
  • Inspect the diff: use git show <commit-hash> to see exact changes.
  • Check the timeline: identify when the regression first appeared.
  • Use a graphical client: visual history can make branch structure easier to understand.
  • Ask the team: if the commit is part of a larger change set, verify it before reverting.

One useful workflow is to compare the suspect commit with the commit immediately before it. If the bug appeared after a specific merge or hotfix, the relevant hash may not be the most recent commit. It may be the one that introduced the bad behavior several steps earlier.

For history inspection, the git log documentation and git show documentation are the most direct references.

Handling Conflicts During a Revert

Conflicts happen when the code you are trying to undo overlaps with later changes. That is normal. Git is telling you it cannot safely apply the inverse of the commit without human input. This is especially common when the reverted code touches heavily edited files such as shared utility functions, configuration files, or feature flags.

When a conflict occurs, Git marks the affected files and pauses the revert. You need to open those files, inspect the conflict markers, and decide what the correct final code should be. Do not just accept one side automatically. The right answer is often a blend of the old state, the reverted change, and the newer work that came after it.

What to do during conflict resolution

  1. Read the surrounding code. Understand what changed after the original commit.
  2. Compare both sides. Decide which parts belong in the final version.
  3. Edit carefully. Remove conflict markers and make the file valid again.
  4. Test immediately. Conflicts are easy to “solve” incorrectly.
  5. Complete the revert. After staging the resolved files, finish the commit.

Testing matters here because a conflict resolution can compile and still be logically wrong. For example, you may restore a function signature correctly but forget that a later commit changed the caller. That kind of subtle breakage is exactly why revert should be followed by a real test run, not just a quick glance.

Warning

Git will not finish the revert until every conflict is resolved and staged. If you abandon the process halfway through, your branch stays in a partially reverted state.

If you want a deeper view of merge and conflict behavior, the git merge documentation is useful because revert conflicts often behave similarly to merge conflicts.

Reverting Multiple Commits or a Range of Changes

Sometimes the bad change is not a single commit. It may be a sequence of commits that together introduced a problem. In that case, you need to think carefully about order and dependencies. Reverting the wrong commit first can create conflicts or remove code that later commits rely on.

When the commits are independent, you can revert them one by one. That is often the safest approach because you can test after each step. If one revert causes a new issue, you know exactly which undo operation created it.

When multiple commits are involved

  • Revert in reverse order: if commits depend on each other, undo the newest first.
  • Test after each revert: do not batch everything blindly if the changes are intertwined.
  • Document the sequence: make sure the team knows what was reverted and why.
  • Watch for hidden dependencies: one commit may look independent but still rely on another.

When the commits are tightly connected, a broader rollback strategy may be better. For example, if three commits introduced a feature flag, UI updates, and API changes together, reverting only one part may leave the system in a broken state. In those cases, your team may need a branch-level rollback plan rather than a single command.

This is where disciplined release management helps. A planned rollback path, clear tagging, and feature-flag strategy make it easier to back out changes cleanly. Revert is one tool in that system, not the entire system.

For more advanced reference behavior, the official Git docs cover how multiple reverts and merge commits behave.

Git Revert vs. Git Reset

The difference between git revert and git reset is one of the most important Git concepts for working developers. git revert preserves history. git reset rewrites history. That one difference determines which command is appropriate in most real-world situations.

git reset is usually better for local, unpublished work. If you are still shaping a branch on your own machine and want to remove a few commits before anyone else sees them, reset can clean up the branch quickly. But once commits are shared, reset becomes risky because it changes commit IDs and can desynchronize teammates.

Git Revert Git Reset
Adds a new commit that undoes earlier changes Moves branch pointers back to an earlier commit
Best for shared branches Best for private work
Does not disturb teammates’ history Can cause divergence if already pushed
Leaves an audit trail Can remove commits from the visible branch history

A useful rule of thumb is simple: if someone else might already have the commit, use revert. If nobody else has the commit and you want to clean up your own local branch, reset may be fine.

The command you choose should reflect the risk to the team, not just your personal preference. That is why many organizations prefer revert on protected branches and restrict force pushes altogether.

For exact behavior, compare git revert with git reset in the official docs.

Git Revert vs. Other Recovery Strategies

git revert is not the only way to recover from a bad change. Sometimes checking out an earlier version of a file, cherry-picking only the good commits, or rolling back a release branch is the better fit. The right choice depends on whether you want to remove one specific change, preserve only selected changes, or back out an entire release candidate.

For a single file, checking out an earlier version can be faster. For example, if one configuration file was edited incorrectly, you may only need to restore that file from a known good commit. That is more targeted than reverting an entire commit that also touched unrelated files.

How the common recovery options differ

  • File checkout: best for restoring one file, not a whole change set.
  • Cherry-pick: useful when you want specific good commits but not others.
  • Branch rollback: better for larger release workflows or staged environments.
  • Tags and hotfix branches: helpful for marking known-good states and patching production quickly.

Cherry-pick can be especially useful when a later branch contains the fixes you want, but an earlier branch includes a bad commit you do not want to bring over. In that case, you can select the commits you need instead of undoing the ones you do not. That is not a substitute for revert, but it is a practical companion strategy.

For release work, some teams prefer tags plus hotfix branches. A tag marks the production baseline, and a hotfix branch carries the emergency fix. Then revert can be used selectively if the hotfix itself causes trouble. This layered approach is more stable than relying on one command for every recovery scenario.

Reference material from the git cherry-pick documentation and git checkout documentation helps clarify these differences.

Best Practices for Using Git Revert

The best revert workflow starts before you type the command. Verify the commit, understand why it exists, and confirm whether teammates are depending on it. A few extra minutes of review can save hours of cleanup later.

Communication matters too. If you are reverting a shared branch, let the team know what is changing and why. That is especially important if the revert affects a deployed service, a release candidate, or code under active testing. Teammates need to know whether they should pull again, retest, or adjust their own local work.

Best practices that prevent avoidable problems

  • Confirm the target: inspect the commit before reverting it.
  • Tell the team: shared branch changes should not be a surprise.
  • Test the result: verify the revert fixed the issue and did not introduce a new one.
  • Write clear commit messages: explain what was reverted and why.
  • Use revert intentionally: it should be part of normal process, not an emergency habit.

Good revert messages are short, factual, and easy to scan. For example, “Revert feature flag change that caused checkout errors” is much better than “Undo stuff.” The message should help someone reading the history six months later understand what happened without opening ten files.

Clear history reduces guesswork. In Git, the next developer often depends on the quality of the last commit message more than the original author expects.

For version-control discipline and secure workflow guidance, see the SANS Institute for practical engineering context and the NIST SSDF for formal software assurance practices.

Common Mistakes to Avoid

Most revert mistakes come from moving too fast. The most common one is reverting the wrong commit because the history was not reviewed carefully enough. Another is forgetting that revert may still create conflicts, especially if the target commit is old or the code has drifted.

Some developers also assume revert deletes history. It does not. That misconception leads to confusion when the original commit still appears in git log. The correct view is that the bad change is still visible, but its effect has been counteracted by a later commit.

Typical errors and how to avoid them

  • Reverting by guesswork: always inspect the diff first.
  • Skipping tests: a clean revert can still break behavior.
  • Using revert for local cleanup: reset may be better before a push.
  • Ignoring later dependencies: older commits may be tangled with newer work.
  • Weak commit messages: make the reason for the revert obvious.

Another subtle mistake is reverting a change that only appears to be the problem. Sometimes the real issue is elsewhere, and the suspected commit is just where the bug surfaced. If you revert the wrong thing, you may mask the symptom without fixing the cause. Use logs, test results, and team context before you act.

If you want a broader view of safe change control, the CISA resources on secure development and operational resilience are worth using as a reference point.

Practical Examples of Git Revert in Real Projects

Consider a shared branch where a new dashboard feature causes a rendering bug in production. The feature was merged yesterday, and several developers already pulled the branch. In that case, reverting the feature commit restores stability quickly while preserving the history that shows exactly what happened.

Now imagine a stakeholder rejects an experimental UI change after a demo. The branch already contains other unrelated improvements that should stay. A revert removes only the UI experiment and leaves the rest of the progress untouched. That is much better than resetting the branch and losing good work with the bad.

Real-world scenarios where revert works well

  • Buggy feature removal: back out the change that broke a shared page or API endpoint.
  • Pre-release rollback: undo one risky change before the release goes live.
  • Partial recovery: revert the commit that introduced a defect while keeping later fixes.
  • Hotfix cleanup: remove a temporary workaround after a more complete fix is merged.
  • Experiment rollback: undo a feature that was useful to test but not ready to keep.

Here is the practical value: revert lets a team preserve development momentum. Engineers do not need to stop and rebuild history from scratch every time one change causes trouble. They can back out the problem, keep moving, and then address the underlying issue in a follow-up branch.

This pattern is common in teams that use release branches, pull request reviews, and CI/CD pipelines. The revert restores operational confidence while the team continues shipping unrelated improvements.

For workflow context, the Git reference manual remains the baseline, while the Red Hat Git overview can help reinforce the practical branch-management perspective.

Conclusion

git revert is the safe way to undo a commit when history must stay intact. It does not erase the original change. It creates a new commit that reverses the effect, which is why it is the right tool for shared branches and collaborative workflows.

Use revert when transparency matters, when teammates may already depend on the branch, and when you want a clean audit trail of both the mistake and the fix. Avoid it when you are still working locally and a simpler reset is more appropriate. In either case, choose the command based on collaboration risk, not convenience.

If you remember one thing, remember this: cancel git revert is not about erasing the past. It is about safely correcting it. Pair the command with careful commit selection, conflict resolution, testing, and clear communication, and you will avoid most of the problems that catch teams off guard.

For more practical Git and DevOps guidance, continue building your workflow around protected branches, tested rollbacks, and documented change control. That is the kind of discipline IT teams at ITU Online IT Training rely on when they need to fix problems without creating new ones.

[ FAQ ]

Frequently Asked Questions.

What does ‘git revert’ do in a Git repository?

Git revert is a command that creates a new commit which undoes the changes introduced by a previous commit. Unlike commands like git reset, which can modify the project history, git revert preserves the commit history by adding a new commit that negates the effects of an earlier one.

This makes git revert particularly useful in collaborative environments where shared branches are involved. It ensures that the history remains consistent and transparent, as every change, including undoing changes, is recorded explicitly. This process minimizes conflicts and confusion among team members working on the same codebase.

When should I use ‘git revert’ instead of ‘git reset’?

Use git revert when you want to undo a commit that has already been shared with others, such as on a main or production branch. It safely creates a new commit that reverses the previous changes without rewriting the commit history.

In contrast, git reset is more suitable for local changes or private branches where rewriting history is acceptable. Reset can remove commits from the branch history, which can cause issues for collaborators if the branch has already been pushed. Therefore, for collaborative workflows, git revert is the safest choice to maintain a clear and consistent project history.

Can I revert multiple commits at once using git revert?

Yes, Git allows you to revert multiple commits in a single command by specifying a range of commits or multiple commit hashes. For example, you can use git revert with the range syntax like ‘git revert OLDEST_COMMIT^..NEWEST_COMMIT’ to undo a series of commits sequentially.

It’s important to note that reverting multiple commits may lead to conflicts if the changes are interdependent. In such cases, resolving conflicts during the revert process is necessary. Additionally, reverting multiple commits creates a series of new commits, each reversing a specific change, which helps maintain a clear history of undo actions.

What are some best practices when using git revert?

When using git revert, it’s best to review the changes thoroughly before executing the command to ensure you’re undoing the correct commit. Additionally, communicate with your team if you’re reverting changes that might impact ongoing work.

Consider reverting commits in a logical order, especially if they are dependent on each other, to prevent conflicts. After reverting, test your code to confirm that the undo operation hasn’t introduced issues. Finally, document the reason for the revert in the commit message for future reference, promoting transparency within the team.

Are there any misconceptions about what ‘git revert’ can do?

A common misconception is that git revert deletes or removes commits from the project history. In reality, it adds a new commit that negates the effects of a previous one, leaving the original commits intact.

Another misconception is that git revert can only undo the most recent commit. In fact, you can revert any commit in the history, provided you specify the correct commit hash or range. Understanding these nuances helps in using git revert effectively within collaborative workflows and complex project histories.

Related Articles

Ready to start learning? Individual Plans →Team Plans →
Discover More, Learn More
What Is (ISC)² CCSP (Certified Cloud Security Professional)? Discover how to enhance your cloud security expertise, prevent common failures, and… What Is (ISC)² CSSLP (Certified Secure Software Lifecycle Professional)? Discover how earning the CSSLP certification can enhance your understanding of secure… What Is 3D Printing? Discover the fundamentals of 3D printing and learn how additive manufacturing transforms… What Is (ISC)² HCISPP (HealthCare Information Security and Privacy Practitioner)? Learn about the HCISPP certification to understand how it enhances healthcare data… What Is 5G? Discover what 5G technology offers by exploring its features, benefits, and real-world… What Is Accelerometer Discover how accelerometers work and their vital role in devices like smartphones,…
FREE COURSE OFFERS