Fundamentals 15 min read

Master Git Undo: Revert, Reset, Rebase, and More

This guide explains common Git undo scenarios—reverting public commits, amending messages, discarding local changes, resetting history, using reflog, branching tricks, interactive rebasing, and stopping file tracking—providing clear commands, underlying principles, and practical tips for safely managing version control mistakes.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Git Undo: Revert, Reset, Rebase, and More

One of the most valuable features of any version‑control system is the ability to "undo" mistakes. In Git, undoing encompasses several related but distinct operations.

When you make a new commit, Git stores a snapshot of the repository at that point; later you can return to an earlier version.

This article covers common scenarios where you need to undo changes and the best Git commands for each.

Undo a Published Change

Scenario: You have pushed commits to GitHub and realize one commit is problematic.

Method: git revert <SHA> How it works: git revert creates a new commit that reverses the changes introduced by the specified SHA. The new commit undoes deletions and removes additions, leaving history unchanged, so you can safely push the “reverse” commit.

Fix the Last Commit Message

Scenario: You made a typo in the most recent commit message ( git commit -m "Fxies bug #42") before pushing.

Method: git commit --amend or git commit --amend -m "Fixes bug #42" How it works: git commit --amend replaces the latest commit with a new one that combines any staged changes with the previous commit; if nothing is staged, only the message is rewritten.

Undo Uncommitted Local Changes

Scenario: Accidental edits were saved locally but not committed.

Method: git checkout -- <bad filename> How it works: git checkout restores files in the working directory to the state recorded in the last commit (HEAD). Uncommitted changes are permanently lost, so verify with git diff first.

Reset Local Commits

Scenario: You have several local commits you want to discard as if they never happened.

Method: git reset <last good SHA> or git reset --hard <last good SHA> How it works: git reset moves the branch pointer back to the specified SHA. Without --hard the working directory is left untouched; with --hard both history and working files are reverted.

Recover After Resetting Local Changes

Scenario: After a hard reset you realize you need the discarded changes.

Method: Use git reflog together with git reset or git checkout.

How it works: git reflog records every change to HEAD, allowing you to recover almost any commit that was once reachable. Note that reflog only tracks HEAD moves, not file‑level checkouts, and it is periodically cleaned.

To restore the repository to a specific point, use git reset --hard <SHA>.

To restore particular files, use git checkout <SHA> -- <filename>.

To re‑apply a specific commit, use git cherry-pick <SHA>.

Branch‑Based Workflow

Scenario: You committed on master but want those commits on a new feature branch.

Method: git branch feature, git reset --hard origin/master, then git checkout feature.

How it works: git branch feature creates the branch pointing at the current commit while staying on master. After resetting master to the remote state, the commits remain on feature, which you then check out.

Rebase to Update Feature Branch

Scenario: Your feature branch was created from an outdated master and you want it based on the latest master.

Method: git checkout feature followed by git rebase master.

How it works: Rebase finds the common ancestor, temporarily stores your feature commits, moves the feature branch to the tip of master, and re‑applies the stored commits on top.

Massive Undo/Selective History Rewrite

Scenario: After many commits you decide only a subset should remain.

Method: git rebase -i <earlier SHA> How it works: Interactive rebase ( -i) opens an editor with a list of commits. You can drop lines to discard commits, change pick to reword to edit messages, or use squash / fixup to combine commits.

Fix an Earlier Commit

Scenario: A past commit missed a file and you haven’t pushed yet.

Method: git commit --squash <SHA> and then git rebase --autosquash -i <even earlier SHA> (or use --fixup).

How it works: git commit --squash creates a new commit with a message like “squash! Earlier commit”. During an autosquash rebase, Git automatically pairs these with the target commit, streamlining the fix.

Stop Tracking a File

Scenario: A log file was accidentally added to the repository; you want Git to stop tracking it.

Method: git rm --cached application.log How it works: .gitignore only ignores untracked files. Once a file is committed, Git continues to monitor it. git rm --cached removes the file from the index while leaving it on disk, so future git status will no longer show it.

Git undo illustration
Git undo illustration
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.

GitrebaserefactoringVersion ControlUNDOresetrevert
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.