Home Coding & Scripting Undoing Changes in Git: restore, reset, revert

Undoing Changes in Git: restore, reset, revert

307
0

Git is powerful because it lets us experiment—try things, make mistakes, and undo them safely. But many beginners get confused when they see multiple ways to undo changes: restore, reset, and revert.

In this post, we’ll learn what each command does, when to use it, and simple examples to help us pick the right one every time.


1. Git restore — Undo Working Directory Changes

Think of git restore as the command that cleans up our pending changes—things we haven’t committed yet.

Use it when:

  • We modified a file but don’t want the change anymore.
  • We staged a file and want to unstage it.

Examples

Undo changes in a file (go back to last commit):

git restore file.txt

Unstage a file (move it back from staged to unstaged):

git restore --staged file.txt

Works on:

  • Our working directory
  • Staging area

Does NOT:

  • Change commit history

2. Git reset — Move HEAD to a previous commit (Dangerous if using –hard)

git reset changes the commit history by moving the branch pointer (HEAD) to an earlier commit.

This affects our local history.

Use it when:

  • We want to undo commits that only exist in our local machine.
  • We want to rewrite or fix local history before pushing.

Three types of reset

1. soft reset (keeps changes staged)

git reset --soft <commit-id>

Good when we want to rewrite last commit but keep changes ready to recommit.

2. mixed reset (default — keeps changes but unstaged)

git reset <commit-id>

3. hard reset (WARNING: discards changes)

git reset --hard <commit-id>

This completely wipes our working directory to match the chosen commit.

Does:

  • Moves branch pointer
  • Can delete commits (locally)

Does NOT:

  • Create a new commit
  • Safe for local-only changes before pushing

3. Git revert — Safely Undo a Commit by Creating a New Commit

If the commit is already pushed to a shared repo, git revert is our safest option.

Instead of deleting old commits, it creates a new commit that reverses the effect of a previous commit.

Use it when:

  • We want to undo changes in shared/public history.
  • We must not break other people’s work.

Examples

Revert the last commit

git revert HEAD

Revert a specific commit

git revert <commit-id>

Does:

  • Create a new commit that undoes earlier work
  • Safe for public branches (like main)

Does NOT:

  • Change old commit history

Summary Table

TaskUse ThisWhy
Undo changes in unstaged/staged filesgit restoreSafe, works on working directory
Undo commits before pushinggit resetRewrites history locally
Undo commits already pushedgit revertSafe for shared repos

Which One Should You Use?

  • If we haven’t committed yet → use restore
  • If we have committed but not pushed → use reset
  • If we have committed and pushed → use revert

Closing Thoughts

Undoing changes in Git doesn’t have to be scary.
Once we understand what each command controls—working directory, staging area, or commit history—we can confidently clean up mistakes without losing our work.

Previous articleUnderstanding Dev Containers in GitHub Codespaces
Next articleUsing Git Stash to Save Work-in-Progress
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here