gsf
gsf

Reputation: 7252

git clean only tracked files

It is an extremely unusual use case, but I would like to remove all tracked files from git repo. Yes, the tracked files, not the untracked ones.

I have a big set of code generation files, some of them binary some of them text files. I would like to be able to track the history of the text generated files. I have the ignore list set, etc. The problem is that the next time I run the generation process it will change the needed files, add the new ones, but it is not going to remove old ones that are not generated anymore. Deleting the whole work-tree will do, but it will also remove the binary and other files that are already there, and I do not want to do that.

If I have the option to use git to clean only the files that are tracked will be the best using already prepared complex .gitignore file to distinguish the files, but I do not see anything in the documentation.

Is there tricky or not an option how to delete from the work-tree all files that are tracked.

Upvotes: 1

Views: 309

Answers (2)

user4003407
user4003407

Reputation: 22132

You can merge empty tree with work tree update:

git read-tree -m -u $(git mktree < /dev/null)

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522551

If you wanted to remove every file under version control in the master branch, then you could try:

git ls-tree -r -z master --name-only | xargs -0 git rm

The first part of the command before the pipe recursively finds all files in master which are versioned, and the git rm removes each one from the remote repository.

git rm <file> removes a file from being tracked by the remote repository.

Upvotes: 1

Related Questions