How To Undo Changes In Git

6 min read

Git is one of those tools that feels empowering right up until the moment you realize you've made a mistake. Maybe you deleted a file you still needed, committed something half-finished, or staged the wrong changes. In real terms, the good news is that undoing changes in Git is one of the system's greatest strengths—once you understand which "layer" of Git your change lives in. This guide walks you through every common scenario, from discarding a single unstaged edit to safely reverting a commit that's already been pushed to a shared repository That alone is useful..

Understanding Git's Three Areas Before You Undo Anything

Before you type a single command, it helps to know where your changes actually live. Git organizes your work into three main areas, and the command you need depends entirely on which area holds the change you want to undo Still holds up..

  • Working directory: the actual files on your disk that you edit in your code editor.
  • Staging area (index): the holding zone where you place changes using git add before committing.
  • Repository (commit history): the permanent record of snapshots created by git commit.

A change moves forward through these areas: working directory → staging area → repository. Which means to undo a change, you move it backward. Understanding this flow is the key that unlocks every command below.

How to Undo Unstaged Changes in the Working Directory

If you've edited a file but haven't staged it yet, you can throw away those edits and restore the file to its last committed state.

Using git restore (modern approach)

git restore filename.txt

This discards your local modifications to that file. To discard changes across all modified files at once, use:

git restore .

Using git checkout (older approach)

git checkout -- filename.txt

Both commands achieve the same result. The git restore syntax is clearer and is the recommended choice in modern Git versions. Be careful: these commands permanently discard your uncommitted edits, so make sure you don't need them before running them.

How to Unstage Changes You've Already Added

Sometimes you've run git add too early and want to pull a file back out of the staging area without losing the edits themselves. This is a common source of confusion, so let's be precise: unstaging keeps your changes, it just removes them from the "to be committed" list.

git restore --staged filename.txt

The older equivalent is:

git reset HEAD filename.txt

To unstage everything at once:

git restore --staged .

After running this, your file returns to the working directory as a modified but unstaged file—exactly where it was before you ran git add Turns out it matters..

How to Fix or Undo Your Most Recent Commit

Amend the last commit

If you committed too early—say you forgot a file or made a typo in the commit message—you can amend the most recent commit instead of creating a new one Small thing, real impact..

git add forgotten-file.txt
git commit --amend

This replaces the previous commit with a new one that includes your additional changes. To change only the message:

git commit --amend -m "A clearer commit message"

Important caveat: only amend commits that haven't been pushed to a shared branch. Amending rewrites history, which can cause headaches for collaborators.

Undo the last commit but keep your changes

If you want to "uncommit" but preserve all the work so you can re-stage it differently, use a soft reset:

git reset --soft HEAD~1

This moves the branch pointer back one commit while leaving your changes staged. A mixed reset (the default) keeps the changes but unstages them:

git reset HEAD~1

Undo the last commit and discard the changes entirely

If you want to erase the commit and the work it contained:

git reset --hard HEAD~1

This is a destructive command. Use it only when you're certain you don't need those changes Simple as that..

How to Undo a Commit That's Already Been Pushed

Here's where things get delicate. And if a commit is already on a shared remote branch, rewriting history with reset is dangerous because it forces everyone else to reconcile diverging histories. The safe, collaborative solution is git revert.

git revert 

git revert doesn't erase the original commit. Because of that, instead, it creates a new commit that applies the exact opposite changes, effectively canceling out the original. This preserves an honest history and won't break anyone else's clone.

git log --oneline

To revert the most recent commit:

git revert HEAD

How to Recover "Lost" Commits with git reflog

One of Git's most reassuring features is that very little is truly lost. Even after a git reset --hard, the commits still exist in Git's internal reference log for a period of time. If you accidentally wiped out work, run:

git reflog

This shows a history of where your HEAD has pointed. Find the commit you want to recover, then reset back to it:

git reset --hard 

The reflog is your safety net—it's the reason a panicked "I destroyed my branch!" moment is usually fixable within minutes Small thing, real impact..

How to Undo a Merge

If you merged a branch and immediately regret it, you can undo the merge before pushing:

git reset --hard HEAD~1

If the merge has already been pushed, revert it safely instead:

git revert -m 1 

The -m 1 flag tells Git which parent of the merge to keep as the mainline.

How to Restore a Deleted File

Deleted a file and committed the deletion? You can bring it back:

git checkout HEAD -- path/to/deleted-file.txt

Or, with the modern syntax:

git restore --source=HEAD path/to/deleted-file.txt

If the deletion is only in your working directory and not yet committed, a simple git restore path/to/file.txt recovers it Practical, not theoretical..

Quick Reference: Which Command Should You Use?

Situation Command
Discard unstaged edits git restore <file>
Unstage a file git restore --staged <file>
Fix the last commit git commit --amend
Uncommit but keep changes git reset --soft HEAD~1
Uncommit and discard changes git reset --hard HEAD~1
Undo a pushed commit safely git revert <hash>
Recover lost commits git reflog then git reset --hard <hash>

Frequently Asked Questions

Does git reset --hard delete my files permanently? Not immediately. The commits remain in the reflog for a while, so you can often recover them. Still, uncommitted changes that were never staged or committed are genuinely gone, so treat --hard with

so treat --hard with caution. Here's the thing — it permanently discards all uncommitted changes in your working directory and resets the branch to the specified commit. If the reset branch has already been pushed to a remote, it will rewrite history for collaborators, potentially causing conflicts That's the whole idea..

Latest Batch

Current Topics

Close to Home

Familiar Territory, New Reads

Thank you for reading about How To Undo Changes In Git. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home