Let's say I have a file named a.txt
. I add it to the staging area, and then I modify it. How could I return it to the way it was when I added it?
4 Answers
- Prior to Git 2.23:
git checkout a.txt
- Starting from Git 2.23:
git restore a.txt
Git tells you this if you type git status
.
Prior to Git 2.23:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
Starting from Git 2.23:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: a
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: a
-
3@Daenyth I've checked it before posting, and you can see the output shows different ways of resetting the files in different states (staged vs. unstaged)– abyxJun 15, 2010 at 15:51
-
1@Daenyth -- you're thinking of 'git checkout branch-name path' or 'git checkout HEAD path' Jun 16, 2010 at 17:58
-
-
Doesn't work for new files, so it doesn't really check out from staging, since it does require an object. How do I checkout from staging? edit It did work with
--
like status says.– RudieMar 7, 2014 at 16:11
git checkout -- a.txt
The other answer on this page doesn't have the --
, and resulted in some confusion.
This is what Git tells you when you type git status
:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
-
4You had been better of telling us the difference, instead of posting what has been quoted before.– BachsauMar 27, 2019 at 4:50
Unstaging a Staged File
The next two sections demonstrate how to work with your staging area and working directory changes. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, let’s say you’ve changed two files and want to commit them as two separate changes, but you accidentally type git add * and stage them both. How can you unstage one of the two? The git status command reminds you:
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
Right below the “Changes to be committed” text, it says use git reset HEAD ... to unstage. So, let’s use that advice to unstage the CONTRIBUTING.md file:
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
The command is a bit strange, but it works. The CONTRIBUTING.md file is modified but once again unstaged.
git restore --staged filename
This is the command to unstage a file after adding it.