ChatGPT解决这个技术问题 Extra ChatGPT

How do I git rm a file without deleting it from disk? [duplicate]

This question already has answers here: Remove a file from a Git repository without deleting it from the local filesystem (13 answers) Closed 8 years ago.

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?

remove it from previous commits or from the last one?
@Nick D. The question let it open.

M
MichaelChirico
git rm --cached file

should do what you want.

You can read more details at git help rm


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.
@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.
I just tested this and it does delete the file from disk
g
gran_profaci

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.


+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.