Search code examples
gitmsysgit

git rm --cached and 'deleted' status


I am wondering why when I do:

git add <file>

and after that, I do:

git rm --cached <file>

The file remains in deleted status in the stage área.

Here the example: enter image description here

I am only looking for an explanation about the 'deleted' status on the file.

Thanks


Solution

  • Try a git reset HEAD yourFile, instead of a git rm --cached.

    A mixed reset will remove your file from the index, without removing it from the working tree.
    See "Undo 'git add' before commit".

    In your case, a git stash would need to precede the git reset, and then a git stash pop would restore your changes in progress, after the reset.


    Regarding the 'deleted' status after a git rm --cached, that command registers in the index the deletion of the file, which is why you see it recorded as 'deleted' for the next commit.

    The OP Ferpega insists:

    I am asking why the deleted status is there as resulting of git rm --cached because this command should has the same behavior than git reset HEAD <file> as you can see in git rm.

    Well, no, a git rm has not the same behavior as a [git reset][8].
    Both will affect the index, but:

    • one (the git rm) will record a file for deletion on the next commit, hence the 'deleted' status,
    • the other (git reset) will copy HEAD to the index, resetting said index back to what the file was in HEAD.