πŸ“˜Git and Github
Estimated reading: 3 minutes 15 views

♻️ Git Undo: How to Undo Changes in Git (Beginner-Friendly Guide with Examples & FAQ)


🧭 Introduction: Git Undo

Made a mistake in Git? Don’t panic! Git provides powerful tools to undo almost anything β€” from local edits to commits and even pushed changes.

This guide explains how to undo:

  • πŸ“ Local changes
  • πŸ’Ύ Staged files
  • πŸ”¨ Commits
  • πŸš€ Pushed commits

πŸ“Œ Why You Need Git Undo Options

πŸ› οΈ ScenarioπŸ”™ Git Undo Tool to Use
Undo uncommitted file changesgit restore, git checkout
Unstage a filegit restore --staged
Undo a commit (keep changes)git reset --soft
Undo a commit (remove changes)git reset --hard
Revert a pushed commit safelygit revert
View history of changesgit log, git reflog

πŸ” Undoing Local File Changes


πŸ”Ή Undo Changes in Working Directory (Not Yet Staged)

git restore filename.txt

πŸ“Œ Restores the file to its last committed state.

πŸ§ͺ Example:

git restore index.html

πŸ”Ή Undo Changes in All Files

git restore .

This restores all modified files in the current directory tree.


βœ… Undoing Staged Changes (Unstage a File)


πŸ”Ή Unstage a Specific File

git restore --staged filename.txt

πŸ§ͺ Example:

git restore --staged login.js

The file stays modified but is removed from the staging area.


πŸ”Ή Unstage All Files

git restore --staged .

πŸ”¨ Undo Last Commit (Keep or Discard Changes)


πŸ”Ή Keep Changes (Undo Commit Only)

git reset --soft HEAD~1

➑️ Moves the last commit back to staging.


πŸ”Ή Discard Changes from Last Commit

git reset --hard HEAD~1

⚠️ This removes the last commit and your code changes. Be careful!


🚫 Undo Multiple Commits

Undo the last N commits and discard all changes:

git reset --hard HEAD~3

Undo the last 3 commits and keep the changes as unstaged:

git reset --mixed HEAD~3

πŸ”™ Undo Pushed Commits Safely


πŸ”Ή Use git revert (Preferred for Shared Repos)

git revert <commit-hash>

πŸ§ͺ Example:

git revert abc1234

This creates a new commit that undoes the changes of the specified commit.


πŸ”Ή Undo Last Commit and Push (Dangerous!)

git reset --hard HEAD~1
git push --force

⚠️ Only do this in your own branches. For shared repos, this may cause conflicts.


πŸ” Viewing History to Undo Precisely


πŸ”Ή Show Commit History

git log

πŸ”Ή Show All Actions (Including Resets)

git reflog

πŸ§ͺ Example Output:

7a8e2c3 HEAD@{0}: commit: Fixed login bug
d45f8e1 HEAD@{1}: reset: moving to HEAD~1
...

You can reset to any previous state:

git reset --hard HEAD@{1}

πŸ“š Summary – Git Undo

Git provides multiple ways to undo mistakes safely, based on your situation and workflow.

πŸ” Key Takeaways:

  • Use git restore to undo file changes (staged or unstaged)
  • Use git reset to undo commits (soft or hard depending on need)
  • Use git revert for safe undos in shared/pushed repositories
  • Use git reflog to view your complete action history

βš™οΈ Real-World Relevance:

These commands are essential for:

  • Debugging mistakes quickly
  • Recovering accidentally deleted work
  • Collaborating safely on shared codebases

❓ Git Undo – Frequently Asked Questions


How do I undo a file edit before staging?

git restore filename.txt

This brings the file back to the last committed version.


How do I unstage a file?

git restore --staged filename.txt

The file will stay modified but is no longer staged.


How do I undo the last commit but keep changes?

git reset --soft HEAD~1

This removes the last commit but keeps your changes staged.


How do I undo changes permanently?

git reset --hard HEAD

⚠️ This discards all unstaged and staged changes.


What is the difference between revert and reset?

CommandUse CaseSafe for Team?
resetRewrites commit history❌ Not safe
revertCreates a new commit that cancels a previous oneβœ… Yes

How do I recover lost commits?

Use:

git reflog

Then:

git reset --hard HEAD@{n}

Replace n with the correct index from reflog output.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Git Undo

Or Copy Link

CONTENTS
Scroll to Top