Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 826
Geo Avatar asked Jun 15 '10 11:06

Geo


People also ask

How do you revert changes in staging area?

If unwanted files were added to the staging area but not yet committed, then a simple reset will do the job: $ git reset HEAD file # Or everything $ git reset HEAD . To only remove unstaged changes in the current working directory, use: git checkout -- .

Which git command moves a file into the staging area?

What Does Git Add Do? git add [filename] selects that file, and moves it to the staging area, marking it for inclusion in the next commit. You can select all files, a directory, specific files, or even specific parts of a file for staging and commit.


2 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 
like image 109
abyx Avatar answered Oct 04 '22 15:10

abyx


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 # 
like image 27
nonrectangular Avatar answered Oct 04 '22 15:10

nonrectangular