I work on a massive git monorepo where hundreds of developers push changes daily.
The repo is huge, and most commits happen on branches I don’t even care about. Unsurprisingly, Git doesn’t perform well at this scale.
Why git fetch
is slow by default
By default, running git fetch
downloads everything from the remote, even branches you’ll never touch.
Instead, if you only fetch the branch you’re working on, e.g. git fetch origin main
, Git only pulls objects related to main
, skipping everything else.
On my machine, this simple change made fetches about 100x faster, just by downloading less.
But what about git pull
?
Here’s the problem:
git pull
(which is really just git fetch
+ git rebase
/merge
) will always fetch all branches by default. That means even if you only care about your current branch, git pull
stays slow.
The fix? Run git pull origin main
.
This fetches only what you need and makes pulls significantly faster.
Making it less tedious
Of course, typing git pull origin main
every single time is painful.
My solution is to create a Git alias that automatically pulls only the current branch (credit to this Stack Overflow post):
|
|
Now, instead of git pull
, just run git p
, and enjoy faster pulls without thinking about it.
Takeaway
If you’re stuck with a giant monorepo, you don’t have to suffer every time you run git pull
.
By narrowing fetches to just the branch you’re on and automating it with an alias, you can get massive speedups with almost no downside.