1

There were a few questions about removing certain files from git history (i.e., Remove a file from a Git repository without deleting it from the local filesystem). However, I can't find a way to remove all the .gitignore'd files from history. The problem is I had a personal local repository and didn't care much about the ignored files, but now I need to share it and it would take forever to upload it to the server because of plenty irrelevant *.csv, *.zip and other files. They are spread between quite a few directories and it would be hard to perform this task one-by-one.

So, is it possible to perform a git rm --cached on all files with certain extensions?

Community
  • 1
  • 1
sashkello
  • 17,306
  • 24
  • 81
  • 109

1 Answers1

0

Yes, this is the sort of task for which git filter-branch was designed. You could do something like this:

git filter-branch --tree-filter 'rm *.csv *.zip' HEAD

It would be best to perform this operation in another clone of your repository, if you want to keep those files untracked in your local working directory.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Hmm... it throws "cannot remove `*.csv': No such file or directory" – sashkello Nov 11 '13 at 01:15
  • I guess git isn't running the command through the shell, so shell expansion isn't happening. Try `--tree-filter 'sh -c "rm *.csv *.zip"'` – Greg Hewgill Nov 11 '13 at 01:16
  • Tried it, same thing. – sashkello Nov 11 '13 at 01:17
  • I just noticed you said they are "spread between quite a few directories" so I guess they're not in the repository root. So you'll have to adjust your command as necessary to delete the files you want. I don't know your directory structure so I can't say exactly what you will need to do. – Greg Hewgill Nov 11 '13 at 01:19
  • So, I need to do something like `rm dir1/dir2/*.zip` or there is a better way to cover all the files? – sashkello Nov 11 '13 at 01:22
  • 1
    See http://stackoverflow.com/questions/7096528/remove-all-of-a-file-type-from-a-directory-and-its-children for an example of how to do that. – Greg Hewgill Nov 11 '13 at 01:25
  • 1
    The "tree filter" *is* run through the shell (`eval`-ed), the problem is that `*.csv` is not matching anything. Also note that filter-branch is kind of a pain to use. Someone wrote [BFG](http://rtyley.github.io/bfg-repo-cleaner/) as an alternative. – torek Nov 11 '13 at 01:49