Vihaan Verma
Vihaan Verma

Reputation: 13143

git rm --cached file vs git reset file

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?

Upvotes: 70

Views: 114202

Answers (2)

Marvin
Marvin

Reputation: 1

For anyone coming here late, slight correction/clarification to @cb-bailey's answer: A file in the index, which is equivalent to "staging area", is considered tracked locally. If a file has been added to the index using git add, i.e. it is marked as tracked, and you do not commit it but use git rm --cached, then it is not a no-op, since it will still remove the file from the index, so that the file ends up being untracked again. The file still exists in your working tree and can be re-added.

On the other hand, git reset is used to, well, reset a file or directory to the current branch's HEAD or a specified commit in your index, while a dedicated mode argument defines what effect will take place on your local working tree.

Upvotes: 0

CB Bailey
CB Bailey

Reputation: 792847

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.

Upvotes: 104

Related Questions