Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

According to the git rm documentation,

--cached Use this option to unstage and remove paths only from the index.     Working tree files, whether modified or not, will be left alone. 

But according to this resource unstaging a file is done with

git reset HEAD <file> 

What is the difference? Is there one?

like image 580
sakurashinken Avatar asked Jun 23 '16 20:06

sakurashinken


People also ask

What is git rm -- cached?

If you just want to stop git from tracking some files or a directory, then use: git rm --cached <filename> this will stop git from tracking the file and "remove it from the index" without removing the file. You probably then want to add the file(s)/path to your . gitignore file to prevent them from being tracked.

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.

Does git rm keep history?

No, git rm will only remove the file from the working directory and add that removal into the index. So only future commits are affected. All previous commits stay the same and the history will actually show when you removed the file from the repository.


2 Answers

With git rm --cached you stage a file for removal, but you don't remove it from the working dir. The file will then be shown as untracked.

Take a test drive

git init test_repo cd test_repo  touch test git add test git commit -m 'Added file test  git rm --cached test  git status Changes to be committed:   (use "git reset HEAD <file>..." to unstage)          deleted:    test      <---- staged for removal  Untracked files:   (use "git add <file>..." to include in what will be committed)          test              <-- still in the working dir 

With git reset <file> you can unstage a file. In the example above you might want to use git reset test to unstage the removal.

git reset test git status On branch master nothing to commit, working directory clean 
like image 128
René Link Avatar answered Oct 06 '22 16:10

René Link


The command with flag git rm --cached removes the file from the index but leaves it in the working directory. This indicates to git that you don't want to track the file any more.

On the other hand, the command git reset HEAD <file> leaves the file as a tracked file in the index, but the modifications cached in the index are lost. This has the effect as if the file in cache had been over written by the file in HEAD (while the working tree file is untouched).

like image 27
Gregg Avatar answered Oct 06 '22 16:10

Gregg