Geo
Geo

Reputation: 96827

How do you revert a git file to its staging area version?

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?

Upvotes: 97

Views: 32451

Answers (3)

Devesh Agarwal
Devesh Agarwal

Reputation: 37

git restore --staged filename

This is the command to unstage a file after adding it.

Upvotes: 0

abyx
abyx

Reputation: 72828

  • 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

Upvotes: 101

nonrectangular
nonrectangular

Reputation: 2119

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
#

Upvotes: 32

Related Questions