gitignore by file size?

22,775

Solution 1

I'm new to .gitignore, so there may be better ways to do this, but I've been excluding files by file size using:

find . -size +1G | cat >> .gitignore

Obviously you'll have to run this code frequently if you're generating a lot of large files.

Solution 2

Although the file size is very large and the following should not be an issue at all and provided that @abendine answer is correct, according to: https://stackoverflow.com/a/22057427/6466510

find * -size +1G | cat >> .gitignore

it would be far better. Have a look at this too: Difference between find . and find * in unix it turns out that replacing . with * here above, avoid to find things in .git directory.

Solution 3

To satisfy github's <100MB file limit, run this:

find . -size +100M | cat >> .gitignore

Solution 4

I wanted to also offer a Windows version of this as well.

forfiles /s /c "cmd /q /c if @fsize GTR 1073741824 echo @relpath" >> .gitignore

Solution 5

(Update 2020-05)

Microsoft released time ago Git-LFS as Open-Source. Probably this is what most people really are searching for:

https://git-lfs.github.com/ C&P from the project page: "Git Large File Storage (LFS) replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise."

Share:
22,775
Warren Benedetto
Author by

Warren Benedetto

I attended Cornell University, earning a Bachelor's degree in Evolution And Human Behavior. I didn't minor in anything, but if I had, it probably would have been something equally useless. After graduation, I moved to Los Angeles to pursue a career in being famous. Upon my arrival, I was informed that there were already enough famous people, and my services would not be needed. Never one to be deterred, I enrolled in USC and earned a Master's degree in TV/Film Writing. "Now can I be famous?" I asked. "No, sorry," Los Angeles said. "But you can be unemployed, if you'd like." Equipped with $200,000 in debt and zero marketable skills, I taught myself graphic design, PHP, MySQL, HTML5, CSS3, JS, UX, UI, NodeJS, and a bunch of other acronyms. Then I convinced some people that I know what all those letters mean. Luckily for me, most companies looking for a designer or developer want someone who is unusually handsome, athletic, and charming. When they can't find anybody like that, they hire me instead. I have 11+ years of experience developing, designing, and marketing web apps and sites from concept to completion. As Creative Director at Gaikai, I helped to launch their groundbreaking cloud gaming service, leading up to their acquisition by Sony Computer Entertainment for $380 million in July 2012. I'm currently their Director Of User Experience working on a project so secret, even I don't know what I'm doing. I'm also the developer of StayFocusd, the popular Google Chrome productivity app. I haven't had any spare time since 1994, but if I did, I would spend it writing screenplays, playing professional basketball, becoming the next great white rapper, and napping.

Updated on January 24, 2022

Comments

  • Warren Benedetto
    Warren Benedetto over 2 years

    I'm trying to implement Git to manage creative assets (Photoshop, Illustrator, Maya, etc.), and I'd like to exclude files from Git based on file size rather than extension, location, etc.

    For example, I don't want to exclude all .avi files, but there are a handful of massive +1GB avi files in random directories that I don't want to commit.

    Any suggestions?

  • aubreypwd
    aubreypwd almost 10 years
    This is a great way to control wp-content/uploads folder when deploying large sites built in WordPress. Thanks for this.
  • IanB
    IanB almost 10 years
    I found I needed to remove the leading ./ from the start of each file before gitignore would work
  • antass
    antass over 7 years
    This is a very old thread, but in case someone stumbled upon it and needed a pastable solution: find . -size +1G | sed 's|^\./||g' | cat >> .gitignore
  • antass
    antass over 7 years
    To avoid storing duplicate file names: find . -size +1G | sed 's|^\./||g' | cat >> .gitignore; awk '!NF || !seen[$0]++' .gitignore. sed will get rid of trailing ./, and awk will remove all duplicate lines ignoring empty ones by checking the number of fields present in a line (NF) - this is useful if your .gitignore is organized in sections separated by empty lines.
  • Sam Watkins
    Sam Watkins over 6 years
    You all win a "useless use of cat award".
  • baxx
    baxx over 4 years
    @SamWatkins do you have an example of how the above could be done without using cat? ( i think that it can just be completely dropped from the above? )
  • Sida Zhou
    Sida Zhou almost 4 years
    lol, took me a second to realize what is the cat award: find . -size +1G | sed 's|^\./||g' >> .gitignore
  • user3710044
    user3710044 almost 4 years
    You don't need sed either ... find . -size +1G -printf '%P\n' works too.
  • Skyborg
    Skyborg over 3 years
    yes they shoud install git lfs and track the big files to automatic exclude them from the repositorys.
  • Robin Kohrs
    Robin Kohrs about 3 years
    Can I do this in some kind of automated way? I mean something like putting a function in my repo (or in my gitignore if this possbile) that continously collects files larger than some size and puts them in the gitignore?
  • Kavidu Aloka Kodikara
    Kavidu Aloka Kodikara about 3 years
    In windows, run the same command and it might add ./ to the start of each path as below ./android/app/build/outputs/apk/debug/app-universal-debug.ap‌​k . Removing the ./ as below solved my issue; android/app/build/outputs/apk/debug/app-universal-debug.apk
  • KIC
    KIC over 2 years
    @RobinKohrs I have added yet another answer using a githook to add some automatic behavior