Fundamentals 7 min read

How to Undo Pushed Git Commits: Revert, Reset & Branch Strategies

This tutorial explains four methods to undo pushed Git commits: manual file restoration, git revert for safe single-commit rollbacks, creating a new branch from an old commit for bulk rollbacks, and hard reset with force push for history rewriting—each with trade-offs for safety and history preservation.

IoT Full-Stack Technology
IoT Full-Stack Technology
IoT Full-Stack Technology
How to Undo Pushed Git Commits: Revert, Reset & Branch Strategies

Introduction

In daily development, developers often push erroneous code to a remote repository or need to revert to a previous version. The author describes a scenario where untested optimizations were pushed but must be removed to accommodate a new requirement on an important project.

Method 1: Manual Comparison and Deletion (Not Recommended)

If the erroneous changes are small, you can compare the current commit with the target commit (e.g., "help文档提交") using the IDE's Compare Versions feature (Ctrl+click two commits). Then manually delete the differing code. This works for simple changes and allows verification by comparing the latest commit with the target commit afterward. However, it becomes impractical for complex changes or configuration files, requiring repeated comparisons.

Method 2: git revert (Recommended for Single Commits)

Right-click the erroneous commit in the Git log and choose Revert . Git automatically creates a new revert commit that undoes the changes introduced by that commit. Push this revert commit to the remote to complete the rollback. This approach is safe because it preserves history and leaves an audit trail. The author notes a drawback: reverting dozens or hundreds of commits one by one is tedious and litters history with many revert commits.

Method 3: Create a New Branch from the Target Commit (Recommended for Many Commits)

Right-click the commit you want to return to (e.g., "help文档提交") and select New Branch . This creates a branch pointing to that commit, leaving the original branch untouched. You can then work on the new branch. This avoids generating numerous revert commits and keeps the old history accessible. Caution: overuse leads to branch management overhead.

Method 4: Reset Current Branch to Target Commit + Force Push (Use with Caution)

Right-click the target commit and choose Reset Current Branch to Here . A dialog offers four modes:

Soft : Keeps your working changes and staged files.

Mixed : Keeps working changes but unstages staged files.

Hard : Resets working directory and index to the selected commit; all changes after that commit are lost (including uncommitted local changes).

Keep : Resets committed changes but preserves uncommitted local changes.

The author selects Hard. After the local reset, the remote still contains the unwanted commits. To synchronize, open the push dialog and enable Force Push (since there are no new commits to push). This overwrites the remote history. Note: force push is blocked on protected branches (e.g., master); the author's branch was unprotected. After force push, the remote shows only the target commit.

Summary Advice

Use git revert for safe, single-commit rollbacks.

Create a new branch when reverting many commits.

Resort to hard reset + force push only when necessary and on unprotected branches, understanding it rewrites history and discards changes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Gitversion controlresetrevertcommit historyforce pushbranch strategy
IoT Full-Stack Technology
Written by

IoT Full-Stack Technology

Dedicated to sharing IoT cloud services, embedded systems, and mobile client technology, with no spam ads.

0 followers
Reader feedback

How this landed with the community

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.