git rm --cached file vs git reset file
Asked Answered
T

1

69

I'm trying to learn Git. I'm confused between

git rm --cached file

and

git reset file

both of the commands seem to take the file from staged to un-staged area. How do the commands differ?

Thermostatics answered 30/9, 2012 at 12:40 Comment(1)
possible duplicate of What's the difference between `git rm --cached asd` and `git reset head -- asd`Packer
C
103

git rm --cached <file> will completely remove the file's contents from the index. This means that on commit the file will be removed from the HEAD commit. (If the file was only added to the index and not yet tracked this is a "no-op".)

git reset -- <file> resets the contents of the file in the index to be the same as the head commit. This means that on commit no changes will be committed to the file. This operation is not valid if there is no tracked version of the file in the HEAD commit.

Criticize answered 30/9, 2012 at 12:46 Comment(6)
Maybe as an addition: git rm --cached will add the delete action of the file to the index, just like git add will add an add action.Ilke
Is index==staging area? If a file is in the index, isn't it tracked by extension?Haunch
For git reset -- <file>, there should be a tracked version of that file in HEAD commit. If it's new file added to index, then there will be no tracked version in HEAD commitHypnology
So git reset doesn't reset the index to what it was right after the last commit, namely empty. It keeps the list of staged files, but resets their contents to that of the last commit. Therefore, git reset isn't the opposite of (doesn't undo) git add.Daveta
is there a way to view files that are in the index? (even if they match the HEAD)Farmland
@bubaka I think you're looking for git ls-files -s. It lists all the files in the index, whether staged or not. What it shows will be what will be put in to your next commit.Vaishnava

© 2022 - 2024 — McMap. All rights reserved.