How to Remove Unstaged Changes in Git: A Quick Guide

While working with Git, you might sometimes make changes to files but then decide you don’t want to keep them. These changes are called unstaged because they haven’t yet been added to the staging area with git add. Knowing how to safely discard these changes can help you avoid clutter and maintain a clean working directory.

This guide explains how to remove unstaged changes in Git—either for specific files or for the entire working directory.


What Are Unstaged Changes?

Unstaged changes are modifications in your working directory that Git has detected but you haven’t staged (via git add) yet. These might be:

  • Edited files not yet staged
  • Deleted or newly created files (if tracked)

How to Remove Unstaged Changes

1. Discard Changes in a Single File

If you want to undo changes in a particular file and revert it to the last committed state:

git restore filename

Example:

git restore app/models.py

2. Discard Changes in Multiple or All Files

To discard unstaged changes in all tracked files:

git restore .

The dot . represents the current directory and all its contents.


3. Removing Untracked Files

Untracked files are new files that Git isn’t tracking yet (like new files you haven’t added).

To list untracked files:

git clean -n

To delete untracked files:

git clean -f

To delete untracked directories too:

git clean -fd

⚠️ Be careful! git clean permanently deletes files and directories.


4. Old Way: Using checkout (Git versions < 2.23)

Before Git 2.23, git checkout was used to discard changes:

git checkout -- filename

This is still valid but git restore is recommended now for clarity.


Important Notes

  • These commands only affect unstaged changes and do not remove staged changes.
  • Discarding changes is irreversible unless you have backups or stashes.
  • Always double-check what you’re discarding to avoid data loss.

Summary Table

ActionCommand
Discard changes in one filegit restore filename
Discard changes in all filesgit restore .
List untracked filesgit clean -n
Delete untracked filesgit clean -f
Delete untracked files + foldersgit clean -fd

Conclusion

Removing unstaged changes helps keep your working directory clean and prevents unwanted modifications from sneaking into commits. Use these commands carefully, and you’ll maintain a tidy Git workflow.

Sharing Is Caring:

Leave a Comment