411

The command removes the file in my system. I meant it to remove only the file from Git-repository.

How can I remove the file from a Git repository, without removing the file in my system?

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

2 Answers2

704
git rm --cached file

should do what you want.

You can read more details at git help rm

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
kenm
  • 23,127
  • 2
  • 43
  • 62
  • 9
    I'm new to Git, but I've done this a few times and then when someone else pulls from the repository, *their* local file is deleted. Still searching to see if way to not delete from next developer that does a pull. – Terry Apr 27 '11 at 18:44
  • 2
    @Terry, see http://stackoverflow.com/questions/2604625/git-how-to-remove-file-from-index-without-deleting-files-from-any-repository – AlcubierreDrive Dec 03 '11 at 04:51
  • 20
    The confusing thing for me is that the man page for git-rm says that it doesn't remove the file(s) from your working directory. But what I see when I don't use --cached is that the file is remved. – zznq Feb 21 '12 at 19:05
  • Wait so... if I do: git rm --cached . It will remove all the files shown to be deleted in my git status? – gran_profaci Jun 25 '13 at 22:41
  • @gran_profaci Presumably that would mark all the files in the current directory as deleted in the repository index without actually deleting the files from the working tree. – kenm Jun 25 '13 at 23:33
  • 1
    Presumably is not a nice word when all you run the risk of all your files being deleted :) – gran_profaci Jun 25 '13 at 23:41
  • 3
    @gran_profaci The presumably was mainly because `git rm` has a `-r` flag to indicate recursive deletes when you give it a directory name like `.`. It isn't a question of whether things will be marked deleted, it's a question of which (or any) things. And `--cached` doesn't actually delete anything. It just marks it as having been done. – kenm Jun 25 '13 at 23:49
  • Ah... thanks a lot for clarifying that! Much appreciated... – gran_profaci Jun 25 '13 at 23:51
  • 2
    I just tested this and it does delete the file from disk – Joe Phillips Feb 23 '17 at 21:49
  • This option should be the default behavior – DrBeco Nov 14 '22 at 00:31
36

I tried experimenting with the answers given. My personal finding came out to be:

git rm -r --cached .

And then

git add .

This seemed to make my working directory nice and clean. You can put your fileName in place of the dot.

gran_profaci
  • 8,087
  • 15
  • 66
  • 99