9

I'm using git to manage a non-coding-project folder that has some large files scattered all over the place.

I'm not interesting in moving the files around as they are not mine, but I know git has issues with very large files (in Linus's words, "we suck at it") and I'd like to avoid those performance issues as much as I can. So here I am, just ran git init and about to hit enter on git add -A but stop just before I do.

I'd like to try to add a flag to git add that selects only certain files. I'm not wanting to be prejudiced based on extension, only based on size. Can add or my .gitignore file do that?

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100

2 Answers2

17

There are no flags to git that will do what you want. However, there are a lot of other utilities available that would work just fine...such as the find command. To add all files smaller than 4 MB:

find * -size -4M -type f -print | xargs git add

This assumes that you have no filenames containing spaces (e.g., "my important file"). If you have such files, then:

find * -size -4M -type f -print0 | xargs -0 git add

UPDATE: Note that I've replaced . with * here, because otherwise it will find (and try to add) things in your .git directory, which is not helpful.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • huh, just curious, what would I use (in the place of `git add` I guess) to count the number of files found matching a minimum size number? – boulder_ruby Feb 27 '14 at 01:57
  • To add dot files that are not dot-git files, you can complextificatify the `find` with stuff like `find . -name .git -prune -o ...`, but usually this is not worthwhile unless you're writing a real script. – torek Feb 27 '14 at 01:59
  • 1
    For the version of the command with `-print`, you can just pipe the output of `find` to `wc -l` (to count lines). – larsks Feb 27 '14 at 02:00
  • apparently you can use the -X option to do the same thing as the `print0` & `-0` thing, according to `man find` – boulder_ruby Feb 27 '14 at 02:24
12

Building off of larsks's answer, you could do the opposite to add the large files to your .gitignore

echo "# Files over 4mb" >> .gitignore
find * -size +4M -type f -print >> .gitignore
Kyle Macey
  • 8,074
  • 2
  • 38
  • 78