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>
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.
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.
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.
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
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With