Unstage a deleted file in git Unstage a deleted file in git git git

Unstage a deleted file in git


Assuming you're wanting to undo the effects of git rm <file> or rm <file> followed by git add -A or something similar:

# this restores the file status in the indexgit reset -- <file># then check out a copy from the indexgit checkout -- <file>

To undo git add <file>, the first line above suffices, assuming you haven't committed yet.


Both questions are answered in git status.

To unstage adding a new file use git rm --cached filename.ext

# Changes to be committed:#   (use "git rm --cached <file>..." to unstage)##   new file:   test

To unstage deleting a file use git reset HEAD filename.ext

# Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)##   deleted:    test

In the other hand, git checkout -- never unstage, it just discards non-staged changes.


If it has been staged and committed, then the following will reset the file:

git reset COMMIT_HASH file_pathgit checkout COMMIT_HASH file_pathgit add file_path

This will work for a deletion that occurred several commits previous.