To undo git add
action, run git restore --staged
for a file or “git reset” for all files that have been added to index. git reset HEAD --
also works for unstaging a file, but take care to use git reset
command for it may bring unexpected results if you do not use it correctly.
Unstage a file
If you add a file to staging area and want to undo the action, use below git restore
command to unstage it:
# Syntax
# Undo add action for a file
#
# --, the following parameter will be parsed as a file path
$ git restore --staged -- <file-path>
# Examples:
# Undo add action for index.php
$ git restore --staged -- index.php
Unstage all files
If you want to undo add
action for all the files that have been added to staging area, use git reset
to do that:
$ git reset HEAD
# or
# If no commit is specified, it defaults to HEAD
$ git reset
If fact, git status
will tell you how to unstage a file. Depending on your version of Git, you may see some content like this :
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: index.md
On older versions, it may prompt using git reset HEAD
to unstage a file:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.php
git reset HEAD --
also work, git reset
and git resotre
has some overlapped function.
If you are confusing the three commands, revert, restore and reset, see their differences at the bottom of the post.
Resources
- git restore
-
There are three commands with similar names:
git reset
,git restore
andgit revert
.-
git-revert is about making a new commit that reverts the changes made by other commits.
-
git-restore is about restoring files in the working tree from either the index or another commit. This command does not update your branch. The command can also be used to restore files in the index from another commit.
-
git-reset is about updating your branch, moving the tip in order to add or remove commits from the branch. This operation changes the commit history.
git reset
can also be used to restore the index, overlapping withgit restore
. -