Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git remove file from repository but leave it on file system

Tags:

git

I try to remove unnecessary files from git repository. That files were initially added and now they are in several branches. What I want is simply stop tracking changes in that files, I don't care what changes should stay there, but I need that files to stay on file system.

I tried following

git filter-branch --index-filter "git rm --cached --ignore-unmatch file_to_remove" HEAD 

but that removed file from file system what is unwanted.

like image 584
michael nesterenko Avatar asked Sep 29 '11 19:09

michael nesterenko


People also ask

How do I remove a file from a git repository?

The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.

Does git rm remove file locally?

git rm will not remove a file from just your working directory. (There is no option to remove a file only from the working tree and yet keep it in the index; use /bin/rm if you want to do that.)


2 Answers

Just:

git rm --cached file [file ...] 

Of course you'll need to make sure the offending files are added to your .gitignore so they don't get recommitted straight away

like image 145
Gareth Avatar answered Sep 22 '22 00:09

Gareth


If you want to keep the file in the repository, you can use:
git update-index --assume-unchanged <fileName>

This keeps the current version of the file in the index, but you can change it all you want and git will ignore those changes.

like image 42
Andy Avatar answered Sep 20 '22 00:09

Andy