Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a file from the staging area (= index = cache) in Git?

Tags:

git

EDIT This question can be understood in two ways, and the optimal answer is different in the two cases.

  • Question 1: I added a previously untracked file to the staging area. How can I remove this file from the staging area without removing it from the file system?

    Answer 1: Use the following command, as described in John Feminella's answer:

    git rm --cached <file> 
  • Question 2: I modified a file already tracked, and added my modifications to the staging area. How can I remove my modifications from the staging area? I.e., how can I unstage my modifications in the file?

    Answer 2: Use the following command, as described in David Underhill's answer:

    git reset <file> 
like image 284
hcs42 Avatar asked Feb 08 '10 16:02

hcs42


People also ask

How do I delete a staged index file?

git rm --cached -r . --cached tells it to remove the paths from staging and the index without removing the files themselves and -r operates on directories recursively. You can then git add any files that you want to keep tracking.

How do I remove a file from a git repository?

The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.

Which command removes file changes from the staging area?

While rm should be employed when removing files from your working directory, effectively erasing a file from existence, git rm will remove files or file modifications from the Git staging index.


1 Answers

You want:

git rm --cached [file] 

If you omit the --cached option, it will also delete it from the working tree. git rm is slightly safer than git reset, because you'll be warned if the staged content doesn't match either the tip of the branch or the file on disk. (If it doesn't, you have to add --force.)

like image 85
John Feminella Avatar answered Sep 22 '22 22:09

John Feminella