How to Undo 'Git Add': Reverting Staged Files Before Commit : A Step-by-Step Guide
Undoing git add
for All Files
If you want to unstage all files you've added, you can use one of the following commands:
- Using a basic
git reset
without any arguments:
git reset
- Specifying
HEAD
with the reset, which refers to the current commit:
git reset HEAD
- Using
git reset
with no arguments, which implicitly targetsHEAD
:
git reset
Any of these commands will unstage all the files you've added, but won't alter the actual changes made to the files.
Undoing git add
for a Specific File
If you only want to unstage a specific file, you can do so by including the file's name in the reset command:
- Basic reset with the filename:
git reset <filename>
- Using
HEAD
with the filename:
git reset HEAD <filename>
- Using
@
(a shortcut forHEAD
since Git v1.8.5) with the filename:
git reset @ <filename>
Replace <filename>
with the actual name of the file you want to unstage.
Understanding the Impact
Using the git reset
command in this way only affects the staging area and doesn't change the contents of your files. It's a safe way to undo the staging before you commit, allowing you to review and reselect the files for your commit.
Note on HEAD
In Git, HEAD
is a reference to the last commit of the current branch. When you use git reset
, you’re essentially telling Git to make the staging area look like the last commit (HEAD
), without altering the working directory.