git undo all uncommitted or unsaved changes
Categories:
How to Undo Uncommitted or Unsaved Changes in Git

Learn various Git commands to discard local modifications, from individual files to entire working directories, before committing them.
Working with Git often involves making experimental changes, trying out new features, or simply making mistakes. Before you commit your work, you might realize that some or all of your local modifications are not what you want to keep. Git provides powerful tools to undo these uncommitted or unsaved changes, allowing you to revert your working directory and staging area to a clean state. This article will guide you through the essential commands to safely discard unwanted changes.
Understanding Git's States: Working Directory, Staging Area, and Repository
Before diving into the commands, it's crucial to understand the three main states of files in Git:
- Working Directory: This is where you make changes to your files. These changes are not yet tracked by Git.
- Staging Area (Index): When you run
git add <file>
, your changes are moved from the working directory to the staging area. This area prepares changes for the next commit. - Local Repository (HEAD): This is where your committed snapshots are stored. When you commit, the changes from the staging area are saved to the repository as a new commit.
Undoing changes primarily involves moving files between these states or discarding them entirely from the working directory or staging area.
graph TD A[Working Directory] -->|Modify files| B{Modified Files} B -->|git add <file>| C[Staging Area] C -->|git commit -m "Message"| D[Local Repository (HEAD)] D -->|git checkout <commit>| A C -->|git reset HEAD <file>| A A -->|git restore <file>| A
Git's workflow: Working Directory, Staging Area, and Local Repository
Discarding Changes in the Working Directory
If you've made changes to files but haven't yet added them to the staging area (git add
), these changes reside solely in your working directory. Git offers a straightforward command to discard these modifications and revert the files to their last committed or staged state.
# Discard changes in a specific file in the working directory
git restore <file_name>
# Alternatively, using the older 'checkout' command (still works)
git checkout -- <file_name>
# Discard all changes in the working directory (untracked files will remain)
git restore .
Commands to discard uncommitted changes in the working directory.
The git restore
command is the modern and recommended way to undo changes in your working directory. It's more intuitive as it clearly states its intent: to restore a file to a previous state. The git checkout -- <file_name>
command achieves the same result but is overloaded with other functionalities, making git restore
a clearer choice for this specific task.
Unstaging and Discarding Changes from the Staging Area
If you've already used git add
to move your changes to the staging area, but haven't committed them yet, you'll need a different approach. You might want to unstage them (move them back to the working directory) or unstage and then discard them entirely.
# Unstage changes for a specific file (moves from staging to working directory)
git restore --staged <file_name>
# Unstage all changes (moves all staged changes back to working directory)
git restore --staged .
# To unstage AND discard changes for a specific file:
# 1. Unstage the file
git restore --staged <file_name>
# 2. Discard changes in the working directory
git restore <file_name>
# To unstage AND discard ALL changes (staged and unstaged):
git restore --staged .
git restore .
Commands to unstage and discard changes from the staging area.
The git restore --staged <file_name>
command moves the specified file's changes from the staging area back to the working directory, making them unstaged. From there, you can either modify them further or discard them using git restore <file_name>
.
Dealing with Untracked Files
Untracked files are new files that Git has not yet started tracking. They are neither in the working directory (in Git's sense of 'tracked' changes) nor the staging area. If you want to remove these files, you need to use git clean
.
git clean
command is powerful and can permanently delete files. Always use the -n
(dry run) option first to see what will be deleted before executing the actual command.# See what untracked files would be removed (dry run)
git clean -n
# Remove untracked files (be careful!)
git clean -f
# Remove untracked files and untracked directories
git clean -fd
# Remove untracked files, untracked directories, and ignored files (e.g., from .gitignore)
git clean -fX
Commands to remove untracked files and directories.
Comprehensive Undo: Resetting to the Last Commit
For a more drastic undo, where you want to discard all changes (staged and unstaged) and revert your entire working directory to the state of the last commit, you can use git reset --hard
.
git reset --hard
will permanently delete all uncommitted changes in your working directory and staging area. There is no undo for this command within Git itself. Use with extreme caution!# Discard all uncommitted changes (staged and unstaged) and untracked files
# This command is very destructive. Ensure you understand its implications.
git reset --hard HEAD
# After 'git reset --hard', you might still have untracked files.
# To remove them as well:
git clean -fd
Using git reset --hard
to revert to the last commit.
The git reset --hard HEAD
command effectively throws away all local changes that haven't been committed. It resets both the staging area and the working directory to match the HEAD
commit. If you also have untracked files, you'll need git clean -fd
afterward to remove them.