Fundamentals 6 min read

How to Undo Pushed Commits in Git: Manual, Revert, New Branch, and Reset Methods

This article explains several practical ways to roll back code that has already been pushed to a remote Git repository, including manual file comparison, using git revert, creating a new branch, and performing a reset with force‑push, while highlighting the advantages and caveats of each approach.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
How to Undo Pushed Commits in Git: Manual, Revert, New Branch, and Reset Methods

When a mistaken commit has been pushed to a remote repository, developers need reliable ways to revert the changes without disrupting ongoing work. This guide walks through four common strategies, from the most manual to the most automated, and shows how to synchronize the corrected state back to the remote.

1. Basic Manual Operation (Not Recommended)

If the erroneous code is small, you can compare the current commit with the target commit, manually delete the unwanted lines, and then commit the cleaned version. In many IDEs you can select two commits, choose Compare Versions , and delete the differences directly. This works for simple changes but becomes cumbersome for complex codebases or configuration files.

2. git revert (Recommended)

For a safer, automated rollback, right‑click the erroneous commit in your IDE and select Revert . Git creates a new revert commit that undoes the changes of the selected commit while preserving history. Push this revert commit to the remote repository to complete the rollback. Note that each revert handles only one commit at a time, which can be tedious if many commits need to be undone.

3. Create a New Branch (Recommended for Large Rollbacks)

When you need to revert many commits, create a new branch from the commit you want to return to. Right‑click the desired commit and choose New Branch . This preserves the original history on the old branch while giving you a clean branch to continue development. Be cautious, as excessive branching can complicate branch management.

4. Reset Current Branch to a Specific Commit (Use with Care)

Use git reset to move the branch pointer to a previous commit. The hard option discards all local changes and resets the working tree to the selected commit. After resetting, you must force‑push ( Force Push ) to update the remote branch, which may be blocked on protected branches. Other reset modes ( soft , mixed , keep ) retain or partially retain local changes, but they are less commonly used for undoing pushes.

All the screenshots in the original article illustrate the IDE steps for each method; the concepts apply equally to command‑line Git usage.

Note: The examples use the 2023 version of IntelliJ IDEA; if you use a different version or prefer the command line, adjust the steps accordingly.

gitVersion Controlbranchresetrevertforce push
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.