Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git -- add all files that are smaller than X megabytes

Tags:

git

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?

like image 918
boulder_ruby Avatar asked Feb 27 '14 01:02

boulder_ruby


2 Answers

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.

like image 107
larsks Avatar answered Sep 28 '22 01:09

larsks


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
like image 36
Kyle Macey Avatar answered Sep 28 '22 02:09

Kyle Macey