How do I undo 'git add' before commit?
Categories:
How to Undo 'git add' Before Committing Changes

Learn various methods to unstage files in Git, effectively reversing the 'git add' command before you create a commit. This guide covers common scenarios and best practices.
The git add
command is a fundamental part of the Git workflow, moving changes from your working directory to the staging area (also known as the index). This prepares them for the next commit. However, it's common to accidentally stage files you didn't intend to, or to stage changes that aren't quite ready. Fortunately, Git provides several straightforward ways to undo git add
and unstage your files before they become part of a commit.
Understanding the Git Staging Area
Before diving into the commands, it's crucial to understand what git add
actually does. It doesn't save your changes permanently; instead, it takes a snapshot of your files and places them in a temporary area called the 'staging area' or 'index'. This area acts as a buffer between your working directory and your local repository. Only changes in the staging area are included when you run git commit
.
flowchart LR A[Working Directory] --> B["git add"] B --> C[Staging Area (Index)] C --> D["git commit"] D --> E[Local Repository] C -- "git restore --staged" --> A
Git workflow illustrating the role of the staging area and how 'git restore --staged' interacts with it.
Method 1: Unstaging Specific Files with 'git restore --staged'
The most common and recommended way to unstage files is using git restore --staged
. This command was introduced in Git 2.23 as a more intuitive alternative to git reset
. It moves the specified files from the staging area back to the working directory, leaving your working directory changes intact.
git restore --staged <file>
Unstage a single file.
git restore --staged file1.txt file2.js
Unstage multiple specific files.
git status
to see a clear list under the 'Changes to be committed' section.Method 2: Unstaging All Files with 'git restore --staged .'
If you've staged many files and want to unstage all of them, you can use the same git restore --staged
command with a dot (.
) to represent all files in the current directory and its subdirectories.
git restore --staged .
Unstage all files in the current directory and subdirectories.
.
as it will unstage everything. Always run git status
first to confirm what's staged.Method 3: Using 'git reset HEAD' (Older Method)
Before Git 2.23, git reset HEAD
was the primary command for unstaging files. While git restore --staged
is now preferred for clarity, git reset HEAD
still works and is good to know, especially if you're working with older Git versions or prefer its syntax. This command effectively tells Git to unstage the specified files, moving them back to the working directory without discarding any changes.
git reset HEAD <file>
Unstage a specific file using 'git reset HEAD'.
git reset HEAD .
Unstage all files using 'git reset HEAD'.
HEAD
in git reset HEAD
refers to the last commit on your current branch. When used with a file path, it unstages that file by making the staging area match the version of the file in HEAD
.Practical Steps to Unstage Files
Here's a step-by-step guide to unstaging files in a typical Git workflow:
1. Check Current Status
Always start by running git status
to see which files are currently staged ('Changes to be committed') and which are unstaged ('Changes not staged for commit').
2. Identify Files to Unstage
From the git status
output, determine which specific files you want to remove from the staging area.
3. Execute Unstage Command
Use git restore --staged <file>
for individual files or git restore --staged .
for all staged files. Alternatively, use git reset HEAD <file>
or git reset HEAD .
.
4. Verify Changes
Run git status
again to confirm that the files have been successfully unstaged and are now listed under 'Changes not staged for commit' or are no longer present if they were new files.