michael nesterenko
michael nesterenko

Reputation: 14449

git remove file from repository but leave it on file system

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.

Upvotes: 37

Views: 10338

Answers (3)

Rob
Rob

Reputation: 1

Checkout and run git clean for each branch.

git-clean - Remove untracked files from the working tree

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

https://git-scm.com/docs/git-clean

Upvotes: -5

Andy
Andy

Reputation: 46534

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.

Upvotes: 2

Gareth
Gareth

Reputation: 138210

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

Upvotes: 59

Related Questions