How to Undo All Changes in Git: A Complete Guide

Version control with Git is powerful—but with power comes the occasional misstep. Whether you made unwanted code edits, staged the wrong files, or committed too soon, Git provides several ways to undo changes at every stage of the development process.

In this guide, we’ll cover how to undo all changes in Git, including unstaged changes, staged files, and committed changes—so you can recover control over your codebase.


🚨 Important Reminder

Undoing changes can be destructive. Always make sure you truly want to discard your changes before running these commands—especially when using commands like reset --hard.


🔁 1. Undo Unstaged Changes (Modified but Not Staged)

If you’ve edited files but haven’t added them with git add yet, and you want to revert them:

git checkout -- .

Or (modern equivalent):

git restore .

This will discard all local changes to tracked files and reset them to the last committed version.


🔄 2. Undo Staged Changes (Files Added with git add)

If you’ve staged files using git add but haven’t committed yet, unstage them:

git reset

To also discard the actual file changes along with unstaging:

git reset --hard

Be cautious: --hard deletes both staged and unstaged changes.


🧹 3. Undo All Local Changes (Staged + Unstaged)

To completely wipe all local changes (back to the last commit):

git reset --hard

Or, to ensure you’re matching the remote branch exactly:

git reset --hard origin/main

Replace main with your branch name if different.


🕰️ 4. Undo the Last Commit (But Keep Changes)

If you committed but realize it was too soon:

git reset --soft HEAD~1

This will remove the last commit but leave your changes staged.

To also unstage the files:

git reset HEAD~1

❌ 5. Delete All Untracked Files and Folders

Untracked files are not in Git yet (e.g., temporary files or build outputs). To delete them:

git clean -fd
  • -f = force
  • -d = remove directories too

Add -n first to preview what will be deleted:

git clean -fdn

🚀 Real-World Example: Reset Your Entire Working Directory

Say you want to reset everything and match exactly what’s in the last commit on main:

git fetch origin
git reset --hard origin/main
git clean -fd

🧠 Summary: What Do You Want to Undo?

SituationCommand
Unstaged changes onlygit restore . or git checkout -- .
Staged changes onlygit reset
All changes (unstaged + staged)git reset --hard
Last commit onlygit reset --soft HEAD~1
Untracked filesgit clean -fd
Match remote exactlygit reset --hard origin/main

✅ Best Practices

  • Use git status to understand your current state before undoing.
  • Create a temporary branch (git checkout -b backup-branch) before risky commands.
  • Use git reflog to recover lost commits if needed.
Sharing Is Caring:

Leave a Comment