417

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?

2
  • remove it from previous commits or from the last one? Commented Aug 13, 2009 at 16:36
  • @Nick D. The question let it open. Commented Aug 13, 2009 at 16:55

2 Answers 2

711
git rm --cached file 

should do what you want.

You can read more details at git help rm

Sign up to request clarification or add additional context in comments.

10 Comments

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.
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.
Presumably is not a nice word when all you run the risk of all your files being deleted :)
@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.
|
37

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.

3 Comments

+1 -r: Allow recursive removal when a leading directory name is given.
-r is not required if you do *.suffix (very disturbing to discover this behavior the hard way)
-r for directories is what was needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.