87

My project is six months old and git is very very slow. We track around 30 files which are of size 5 MB to 50 MB. Those are binary files and we keep them in git. I believe those files are making git slow.

Is there a way to kill all files of size > 5MB from the repository. I know I would lose all of these files and that is okay with me.

Ideally I would like a command that would list all the big files ( > 5MB) . I can see the list and then I say okay go ahead and delete those files and make git faster.

I should mention that git is slow not only on my machine but deploying the app on staging environment is now taking around 3 hours.

So the fix should be something that will affect the server and not only the users of repository.

Simon East
  • 55,742
  • 17
  • 139
  • 133
Nick Vanderbilt
  • 36,724
  • 29
  • 83
  • 106

10 Answers10

127

Do you garbage collect?

git gc

This makes a significant difference in speed, even for small repos.

kubi
  • 48,104
  • 19
  • 94
  • 118
  • 9
    This is done automatically when there gets to be too much clutter. I doubt it'll really help the OP. – Cascabel Jun 16 '10 at 19:01
  • @Jefromi, is that new? I just upgraded to 1.7.1 yesterday, but previous to that the version I was using definitely did not automatically run `gc`. – kubi Jun 16 '10 at 20:46
  • @kubi: Well, it hasn't been around forever, but it's not exactly new - it's been invoked from commit, merge, am, and rebase since caf9de2 (Sep 14 2007), or in stable version v1.5.4 (Feb 1 2008). – Cascabel Jun 17 '10 at 13:24
  • if it's called during commit, then what's the point in ever running it manually? Have I been imagining the speed increases I think I've seen, or is the auto-invoke somehow different that the manually invoked call? – kubi Jun 17 '10 at 13:40
  • 1
    On second thought, `git gc` can't possibly be called on `commit` and `merge`, otherwise `git fsck --unreachable` would never return anything. – kubi Jun 17 '10 at 13:51
  • 4
    Found it. The default number of loose objects before the auto `gc` runs is 6700, which explains why I've never seen it run. – kubi Jun 17 '10 at 14:01
  • Took a while to run and the difference is quite noticeable when using the GitX Mac app. – HostedMetrics.com Aug 01 '14 at 16:18
  • I love the magical: - One Line, Does All - answers in StackOverflow! – hatef Jul 11 '15 at 15:28
82

Explanation

Git is really good at huge histories of small text files because it can store them and their changes efficiently. At the same time, git is very bad at binary files, and will naïvely store separate copies of the file (by default, at least). The repository gets huge, and then it gets slow, as you've observed.

This is a common problem among DVCS's, exacerbated by the fact that you download every version of every file ("the whole repository") every time you clone. The guys at Kiln are working on a plugin to treat these large files more like Subversion, which only downloads historical versions on-demand.

Solution

This command will list all files under the current directory of size >= 5MB.

find . -size +5000000c 2>/dev/null -exec ls -l {} \;

If you want to remove the files from the entire history of the repository, you can use this idea with git filter-branch to walk the history and get rid of all traces of large files. After doing this, all new clones of the repository will be leaner. If you want to lean-up a repository without cloning, you'll find directions on the man page (see "Checklist for Shrinking a Repository").

git filter-branch --index-filter \
    'find . -size +5000000c 2>/dev/null -exec git rm --cached --ignore-unmatch {} \;'

A word of warning: this will make your repository incompatible with other clones, because the trees and indices have different files checked in; you won't be able to push or pull from them anymore.

David G
  • 94,763
  • 41
  • 167
  • 253
Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
  • 4
    Note: that's the Unix/Linux version of find, not the Windows find.exe. – Craig Trader Jun 16 '10 at 17:31
  • 1
    +1. Might want to send the output of `find` to a file first, check the list, then use `git rm`, just in case there are any false hits. Alternatively, check `git status` after removing large files, and use `git checkout HEAD ` to get back any mistakenly removed files. – Cascabel Jun 16 '10 at 19:03
  • 3
    I think your comment that git "stores separate copies by default" is backwards. According to the email chain you linked to (http://thread.gmane.org/gmane.comp.version-control.git/146957/focus=147598) by default, git _tries_ to diff the binary files -- and that's what's causing the issue; not the storage. – Alexander Bird Apr 24 '13 at 03:47
17

Here is a censored revision intended to be less negative and inflammatory:

Git has a well-known weakness when it comes to files that are not line-by-line text files. There is currently no solution, and no plans announced by the core git team to address this. There are workarounds if your project is small, say, 100 MB or so. There exist branches of the git project to address this scalability issue, but these branches are not mature at this time. Some other revision control systems do not have this specific issue. You should consider this issue as just one of many factors when deciding whether to select git as your revision control system.

John
  • 187
  • 2
  • 8
    "Git has a well known weakness..." - citation needed – Nav Mar 05 '14 at 10:33
  • 7
    I know it. who needs quotes when its actual common knowledge. just dont use git for binary. use perforce or specialized asset management. – v.oddou Aug 26 '14 at 13:03
  • 1
    @v.oddou Well, there is a difference between "I know it" and "its actual common knowledge". This thing is that not everybody knows it and probably it's not even completely true. So any kind of citation improves this answer. It's okay but surely not outstanding and backed up. – NoDataDumpNoContribution Feb 14 '15 at 21:42
  • 3
    Well, not to add fuel to the fire, but if you do a google search for "git and binary files slow", there are a lot of links that are found which report users having problems managing binary files in git. Also, developers who use one SCM or another know the strengths and weaknesses of each system... so, git has developed a reputation of becoming really slow when binary files are thrown into a repo. – AhiyaHiya Mar 30 '16 at 17:16
  • it's in all introductory resources I've used that git is bad with binary files. git-annex exists to fix this. git is great, but not for binary data. It would be good to link to forks adding binary features, so people can support the work. – fuzzyTew Sep 26 '20 at 13:52
16

There is nothing specific about binary files and the way git is handling them. When you add a file to a git repository, a header is added and the file is compressed with zlib and renamed after the SHA1 hash. This is exactly the same regardless of file type. There is nothing in zlib compression that makes it problematic for binary files.

But at some points (pushing, gc) Git start to look at the possibility to delta compress content. If git find files that are similar (filename etc) it is putting them in RAM and starting to compress them together. If you have 100 files and each of them arr say 50Mb it will try to put 5GB in the memory at the same time. To this you have to add some more to make things work. You computer may not have this amount of RAM and it starts to swap. The process takes time.

You can limit the depth of the delta compression so that the process doesn't use that much memory but the result is less efficient compression. (core.bigFileThreshold, delta attribute, pack.window, pack.depth, pack.windowMemory etc)

So there are lots of thinks you can do to make git work very well with large files.

martin
  • 281
  • 3
  • 3
  • 4
    See [here](http://thread.gmane.org/gmane.comp.version-control.git/146957/focus=147598) for an explanation of how to disable those "delta" attempts from happening. – Alexander Bird Apr 24 '13 at 03:46
6

One way of speeding things up is to use the --depth 1 flag. See the man page for details. I am not a great git guru but I believe this says do the equivalent of a p4 get or an svn get, that is it give you only the latest files only instead of "give me all of the revisions of all the files through out all time" which is what git clone does.

DarkAjax
  • 15,955
  • 11
  • 53
  • 65
David
  • 61
  • 1
  • 1
4

You can also consider BFG Repo Cleaner as a faster easier way to clean up large files.

https://rtyley.github.io/bfg-repo-cleaner/

David I.
  • 4,747
  • 3
  • 26
  • 34
4

have you told git those files are binary?

e.g. added *.ext binary to your repository's .gitattributes

sml
  • 2,160
  • 13
  • 12
2

I have been running Git since 2008 both on windows and GNU/linux and I most of the files I track are binary files. Some of my repos are several GB and contains Jpeg and other media. I have many computers both at home and at work running Git.

I have never had the symptoms that are described by the original post. But just a couple of weeks ago I installed MsysGit on an old Win-XP Laptop and almost whatever I did, it brought git to a halt. Even test with just two or three small text files was ridiculously slow. We are talking about 10 minutes to add a file less that 1k... it seems like the git processes stayed alive forever. Everything else worked as expected on this computer.
I downgraded from the latest version to 1.6 something and the problems were gone...
I have other Laptops of the same brand, also with Win-XP installed by the same IT department form the same image, where Git works fine regardless of version... So there must be something odd with that particular computer.

I have also done some tests with binary files and compression. If you have a BMP picture and you make small changes to it and commit them, git gc will compress very well. So my conclusion is that the compression is not depending on if the files are binary or not.

martin
  • 169
  • 1
  • 2
-2

Just set the files up to be ignored. See the link below:

http://help.github.com/git-ignore/

joshlrogers
  • 1,136
  • 1
  • 11
  • 20
  • @Jefromi actually if you look at the link I posted you'll see that there is instructions in the second paragraph telling him exactly what to do in that case. – joshlrogers Jun 16 '10 at 20:05
  • 14
    True. But the direct content of your answer is "ignore the files", not "remove the files from tracking then ignore them". It's generally better to write it here than to link to another site. – Cascabel Jun 16 '10 at 20:30
-22

That's because git isn't scalable.

This is a serious limitation in git that is drowned out by git advocacy. Search the git mailing lists and you'll find hundreds of users wondering why just a meager 100 MB of images (say, for a web site or application) brings git to its knees. The problem appears to be that nearly all of git relies on an optimization they refer to as "packing". Unfortunately, packing is inefficient for all but the smallest text files (i.e., source code). Worse, it grows less and less efficient as the history increases.

It's really an embarrassing flaw in git, which is touted as "fast" (despite lack of evidence), and the git developers are well aware of it. Why haven't they fixed it? You'll find responses on the git mailing list from git developers who won't recognize the problem because they Photoshop documents (*.psd) are proprietary format. Yes, it's really that bad.

Here's the upshot:

Use git for tiny, source-code only projects for which you don't feel like setting up a separate repo. Or for small source-code only projects where you want to take advantage of git's copy-the-entire-repo model of decentralized development. Or when you simply want to learn a new tool. All of these are good reasons to use git, and it's always fun to learn new tools.

Don't use git if you have a large code base, binaries, huge history, etc. Just one of our repos is a TB. Git can't handle it. VSS, CVS, and SVN handle it just fine. (SVN bloats up, though.)

Also, give git time to mature. It's still immature, yet it has a lot of momentum. In time, I think the practical nature of Linus will overcome the OSS purists, and git will eventually be usable in the larger field.

John
  • 243
  • 1
  • 1
  • 16
    This answer is really overly negative and inflammatory. Yes, git has scalability problems *with binary files*. It's quite scalable and fast for code. There's plenty of evidence of the speed (despite your assertion the the contrary), even disregarding the fact that CVS/SVN require network access instead of disk access for many operations. There are many large projects with huge histories quite happily using git. – Cascabel Jun 16 '10 at 19:09
  • 9
    And... your harping on the Photoshop thing? I'm not going to waste my time writing a detailed response, but if from reading the entire thread http://thread.gmane.org/gmane.comp.version-control.git/146957/focus=147598 (maybe you're annoyed because the John in the thread is you?), I see a lot of reasonable responses about how best to handle this with current git, how it might be addressed in the future, and why it's not their first priority. – Cascabel Jun 16 '10 at 19:14
  • 15
    Yeah, I don't think you're right, here. Git works _way_ too well for the Linux kernel to deserve a dismissive, "isn't scalable." – Andres Jaan Tack Jun 16 '10 at 19:58
  • 1
    This comment would be more believable if it had links or data to back it up. BTW, what do you think of mercurial? – vy32 Jun 16 '10 at 21:24
  • LoL, very very funny. amazing answer. GIT ISN'T SCALABLE . i still laughing – pylover Nov 28 '14 at 22:14
  • 5
    Maybe he doesn't express a popular opinion, but I think he the down-voting was more excessive in it's 'negativity' than the OP's Answer. We should encourage dissent, not pile on just because someone doesn't like the version control flavor of the year. GIT really isn't well suited for tracking binary files. But it works great for source code, it's primary intent, which is why it does wonderful at the linux kernel. – dyasta Mar 25 '16 at 22:57
  • 2
    @jeremy.collake It's called groupthink... Popular opinion says git is the best thing ever, so we all have to believe it or we're just plain wrong. Except that what John is saying here is absolutely right. Git does not scale well with large files. – Andrew Oct 24 '17 at 05:11
  • And a large number of negative words that StackOverflow won't let me say to everyone who downvoted his answer. – Andrew Oct 24 '17 at 05:12
  • It seems I've been pummeled too. I have a situation where I have a large meta-repository (I know, not ideal), but it is slow as molasses with Git, but was fast with SVN. Call me crazy, but I think I prefer SVN. I'm staying with Git just to adhere to the 'industry standard', but there is so much in Git that if you don't need, then ... well... it offers little more. – dyasta Oct 25 '17 at 02:24