Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 687
Vihaan Verma Avatar asked Sep 30 '12 12:09

Vihaan Verma


People also ask

What is the difference between git rm -- cached and git reset?

git rm —cached file will remove the file from the stage. That is, when you commit the file will be removed. git reset HEAD — file will simply reset file in the staging area to the state where it was on the HEAD commit, i.e. will undo any changes you did to it since last commiting.

What is git rm -- cached file?

The Git rm –cached flag removes a file from the staging area. The files from the working directory will remain intact. This means that you'll still have a copy of the file locally. The file will be removed from the index tracking your Git project.

How do I revert git rm cached?

If you've run only git rm -r --cached , try doing a git reset HEAD . from within your repo root. If you did a git commit -m "msg" after doing a git rm -r --cached , i.e., you committed the changes, then do git reset HEAD~1 to undo your last commit.

What is the use of git rm?

git rm will remove the file from the index and working directory ( only index if you used --cached ) so that the deletion is staged for next commit.


1 Answers

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.

like image 148
CB Bailey Avatar answered Sep 22 '22 00:09

CB Bailey