October 7, 2019

How to Use Git Shallow Clone to Improve Performance

Git at Scale
Version Control

Cloning an entire repo is standard operating procedure using Git. Each clone usually includes everything in a repository.

That means when you clone, you get not only the files, but every revision of every file ever committed, plus the history of each commit. And if your repo has a lot of assets, that is going to include a long history. The primary issue is that this will create a bottleneck in Continuous Integration (CI) pipelines.

Solution: Git Shallow Clone

Git shallow clone lets you pull down just the latest commits, not the entire repo history. So if your project has years of history, or history from thousands of commits, you can select a particular depth to pull.

How to Execute Git Shallow Clone

Provide an argument of -- depth 1 to the git clone command to copy only the latest revision of a repo:

git clone -–depth [depth] [remote-url]

 

You can also use git shallow clone to access a single branch:

git clone [remote-url] --branch [name] --single-branch [folder]

 

With git shallow clone you get fewer files. And as a result, they clone faster. Builds and feedback can be delivered quicker.

Solution: Completely Remove Unwanted History

If you don’t need to maintain a full history, you can use the following set of commands to remove the history entirely from your Git repo. With the repository cloned into a path on your workstation, use the --orphan option, which returns it to the init state with only one commit.

How to Prune Your Repos

git checkout --orphan freshBranch
git add -A
git commit
git branch -D master 
git branch -m master 
git push -f origin master 
git gc --aggressive --prune=all
git push -f origin master

Add all the files in the path and commit. Next, you delete the remote master branch, rename the current branch to master. Then force push your new master to the code hosting environment. Finally, remove all the old files with the prune command, and push the new state to the remote.

By pruning your repos, you can improve cloning performance without needing to use git shallow clone. Eliminating unwanted history lightens your repo, allowing it be delivered faster.

How to Really Improve Git Performance

Using git shallow clone can help manage large repos with a lot of history, even after pruning your repo. But it is a temporary fix. There is a better way. To manage your large repos, and deliver feedback to developers wherever they are located, there is Helix4Git.

Helix4Git Solves Git Performance Issues

Helix4Git allows you to store all your Git repos in the high-performance Perforce server. Your teams can still clone, push, and pull code using Git. But unlike with native Git, developers get their feedback faster without needing to use git shallow clone.

That’s because using advanced replication, DevOps teams can quickly pull assets into their build pipeline. Customers who try Helix4Git experience 80% faster builds, that take up 18% less storage.

And Helix4Git can also has an intelligent proxy cache functionality for remote sites. This allows everyone at the remote site to work as though they are sitting in the same room with the master Git server.

Get started with Helix4Git.

CONTACT US

 

Want to learn more? Explore Git best practices.