Git Discard Changes Explained (Undo, Remove, Reset All Changes with Examples)

Git Discard Changes Explained (Undo, Remove, Reset All Changes with Examples)

Discarding changes in Git is a common task when you want to undo changes, remove local changes, or reset your repository to a clean state.

In Git, you can discard changes in multiple ways depending on where they exist — whether they are unstaged, staged, or already committed. Common commands used include git restore, git reset, git clean, and git stash.

In this guide, you will learn:

  • How to discard changes in Git
  • How to discard local changes safely
  • How to remove all changes in Git
  • How to undo changes using different Git commands

This tutorial covers real-world scenarios and best practices to help you safely discard changes without losing important work.


Git Discard Changes - Quick Cheat Sheet

TaskCommand
Discard unstaged changes (all files)git restore .
Discard unstaged changes (specific file)git restore <file>
Discard staged changes (unstage file)git restore --staged <file>
Unstage all changesgit restore --staged .
Discard all local changes (staged + unstaged)git reset --hard HEAD
Remove untracked filesgit clean -f
Remove untracked files and directoriesgit clean -fd
Preview files to be deleted (safe mode)git clean -n
Temporarily save changes (safe discard)git stash
Restore stashed changesgit stash apply
Drop stashed changes permanentlygit stash drop
Undo last commit but keep changesgit reset --soft HEAD~1
Undo last commit and unstage changesgit reset --mixed HEAD~1
Undo last commit and delete changesgit reset --hard HEAD~1
Revert a committed change safelygit revert <commit>
Discard changes in a file from last commitgit restore --source=HEAD <file>
Discard changes from a specific commitgit restore --source=<commit> <file>
Remove all changes and reset repogit reset --hard && git clean -fd

What Does "Discard Changes" Mean in Git?

In Git, discarding changes means removing modifications that you no longer want to keep in your repository. These changes can exist in different stages such as the working directory (unstaged), staging area (staged), or even committed history.

Depending on where the changes exist, Git provides different commands like git restore, git reset, git clean, and git revert to safely or permanently discard them.

Discarding changes in Git means undoing modifications from working directory, staging area, or commit history using appropriate commands.

Working directory vs staging vs commits

To properly discard changes, you must understand Git’s three main states:

  • Working Directory (Unstaged Changes)
    Files you modified but haven’t added using git add
    → Use: git restore

  • Staging Area (Index / Staged Changes)
    Files added using git add but not committed using git commit
    → Use: git restore --staged or git reset

  • Commit History (Committed Changes)
    Changes already saved in Git history using git commit
    → Use: git revert or git reset

When you should discard changes vs keep them

You should discard changes when:

  • Changes are experimental or incorrect
  • You want to revert to last working version
  • You accidentally modified or staged files
  • You want to clean your working directory

You should NOT discard changes when:

  • Changes are important but incomplete → use git stash
  • You are unsure → take backup or stash first
  • Changes are already pushed → use git revert instead of reset

Git Discard vs Remove vs Undo Changes

In Git, different users use different terms for the same action:

  • Discard changes in Git → Remove unwanted modifications
  • Remove changes in Git → Delete local updates
  • Undo changes in Git → Revert to previous state
  • Delete changes in Git → Permanently remove modifications

All these actions can be performed using commands like


Understanding the Git Discard Workflow

Git discard changes workflow

The above workflow diagram simplifies how to decide which Git command to use when discarding changes:

  1. Start with unwanted changes

    • Files you modified (after using git add or before committing with git commit) and now want to discard them
  2. Check if changes are staged

    • If No (unstaged changes):

      • Use:
        • git restore . → discard all changes
        • git restore <file> → discard specific file
        • git clean -f → remove untracked files
    • If Yes (staged changes):

      • Use:
        • git restore --staged <file> → unstage changes
        • git reset --hard → discard everything (dangerous)
  3. If changes are already committed

    • Use:
      • git revert <commit> → safe undo
      • git reset --hard → remove commits (use carefully)
      • git stash → temporarily save changes

Key Insight:
The correct command depends on where your changes exist (unstaged, staged, or committed). Using the wrong command can lead to permanent data loss, especially with git reset --hard.


How to Discard Unstaged Changes in Git

Unstaged changes are modifications in your working directory that have not been added using git add.

Discard changes in a specific file (git restore )

To discard changes in a single file:

bash
git restore app.py

This will:

  • Restore the file to its last committed state
  • Remove all local modifications

Quick Tip:
Use this when you made unwanted edits to a file and want to revert it.

Discard all local changes (git restore .)

To discard all unstaged changes in the repository:

bash
git restore .

This will:

  • Reset all modified files
  • Keep staged and committed changes untouched

Discard Staged Changes (Unstage Changes)

Staged changes are files added using git add and ready to be committed.

Remove file from staging area (git restore --staged)

To unstage a specific file:

bash
git restore --staged app.py

This will:

  • Move the file from staging area → working directory
  • Keep your changes intact

Unstage all changes safely

To unstage all files:

bash
git restore --staged .

Alternative (older command):

bash
git reset
IMPORTANT
This does NOT delete changes — it only removes them from staging.

How to Discard All Local Changes in Git (Reset Everything)

This is used when you want to completely clean your working directory.

Use git reset --hard HEAD

bash
git reset --hard HEAD

This will:

  • Remove all staged and unstaged changes
  • Reset repository to last commit
WARNING
This action is irreversible (data loss possible)

Difference between reset --soft, --mixed, --hard

CommandWhat it does
git reset --softMoves HEAD, keeps staged + working changes
git reset --mixedUnstages changes, keeps working directory
git reset --hardDeletes all changes (working + staged)

Quick Insight:

  • Use --soft → undo commit only
  • Use --mixed → unstage changes
  • Use --hard → delete everything

How to Remove Untracked Files in Git (git clean)

Untracked files are files not added to Git (not part of repo).

Use git clean -f and -fd

Remove untracked files using git clean

bash
git clean -f

Remove untracked files and directories:

bash
git clean -fd

Safe dry run using git clean -n

Before deleting files, always preview:

bash
git clean -n

This shows:

  • Which files will be removed
  • Helps avoid accidental deletion

Best Practice:
Always run git clean -n before git clean -f


Temporarily Discard Changes (Safe Method)

Sometimes you don’t want to permanently delete changes — instead, save them temporarily using git stash—you just want to set them aside temporarily.

Use git stash to save changes

You can use git stash:

bash
git stash

This will:

  • Save both staged and unstaged changes
  • Clean your working directory

Add a message:

bash
git stash save "WIP changes"

Restore stashed changes later

View stashes:

bash
git stash list

Apply stash:

bash
git stash apply

Apply and remove stash:

bash
git stash pop

Delete stash:

bash
git stash drop

Best Use Case:
When switching branches or testing something without losing work.


Undo Already Committed Changes

When changes are already committed, you must use different approaches.

Use git revert (safe for shared repos)

bash
git revert <commit>

This will:

  • Create a new commit that undoes previous changes
  • Preserve Git history (safe for teams)

Recommended for:
Shared repositories and pushed commits

Use git reset (dangerous but powerful)

bash
git reset --hard HEAD~1

This will:

  • Remove the last commit completely
  • Delete associated changes
WARNING
Do NOT use on pushed commits unless you understand the impact.

Discard Changes for a Single File Only

Useful when you want to discard changes in just one file instead of the entire repo.

Restore file to last commit

bash
git restore app.py

This will:

  • Discard all changes in the file
  • Restore from last commit (HEAD)

Restore file from specific commit

bash
git restore --source=<commit> app.py

Example:

bash
git restore --source=HEAD~1 app.py

This helps:

  • Recover older file versions
  • Compare and selectively restore

Common Mistakes While Discarding Changes (High Ranking Section)

Difference between restore, reset, checkout confusion

CommandPurpose
git restoreDiscard changes (recommended modern command)
git resetUnstage or reset commits
git checkoutOlder method (now replaced by restore/switch)

Key Tip:
Use git restore for discarding changes instead of checkout.

Why changes are not getting discarded

Common reasons:

  • Changes are already staged → use --staged
  • Changes are committed → use reset or revert
  • File is untracked → use git clean
  • Wrong command used for the state

Check current state using git status:

bash
git status

Risk of git reset --hard (data loss)

bash
git reset --hard
HINT

This will:

  • Delete all local changes permanently
  • Cannot be undone easily

Safer alternative:

bash
git stash

Golden Rule:
If unsure → stash first, then proceed


Frequently Asked Questions

1. How to discard changes in Git?

You can discard changes in Git using commands like git restore, git reset, git checkout, or git clean depending on whether changes are unstaged, staged, or untracked.

2. How to discard local changes in Git?

Use git restore . to discard all local changes in the working directory or git restore <file> to discard changes in a specific file.

3. How to discard all changes in Git?

Use git reset --hard HEAD to discard all staged and unstaged changes. Be careful, as this will permanently remove all local modifications.

4. How to discard unstaged changes in Git?

Use git restore <file> or git restore . to discard unstaged changes in files.

5. How to discard staged changes in Git?

Use git reset <file> or git restore --staged <file> to unstage changes.

6. How to delete untracked files in Git?

Use git clean -f to remove untracked files and git clean -fd to remove untracked directories as well.

7. What is the safest way to undo changes in Git?

The safest way is using git stash to temporarily save changes or git revert to undo committed changes without rewriting history.

8. What is the difference between git reset and git restore?

git reset is used to unstage changes or reset commits, while git restore is used to discard changes in the working directory or staging area in a safer and more intuitive way.

Summary

Discarding changes in Git depends on where your changes exist in the workflow involving git add, git commit, and cleanup commands like git clean

  • Unstaged changesgit restore
  • Staged changesgit restore --staged or git reset
  • Untracked filesgit clean
  • Committed changesgit revert or git reset
  • Temporary discardgit stash

Understanding these scenarios helps you choose the right command and avoid data loss.


Official Documentation

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.