A shallow clone is a git clone of only part of a repository’s git commit history. There are three ways that we found that this can be done. This can be useful in situations where you need to download only part of the files in a large git repository. As a bonus we also cover how to fetch a single commit and partial clones.
If you want to improve the overall speed of git commands then we suggest you upgrade to the latest versions which have significant improvements done to the commit-graph.
Three ways To Shallow Clone
The only prerequisite is that you have at least git version 1.9 and the command is basically as follows:
git clone <repository> --depth 1
This will download only the latest commits instead of the entire repository. This should make things considerably faster. If you are using a newer version of git 2.11.1 you can also use shallow-exclude with a remote branch or tag.
git clone <repository> --shallow-exclude=<remote branch or tag>
Or
git clone <repository> --shallow-since=YYYY-MM-DD
Bonus: Fetch A Single Commit
As from git version 2.5 you are allowed to fetch a single commit. git-upload-pack which is typically not used by the user but runs as part of git fetch. You can use git clone to get a shallow clone in combination with git-upload-pack and the SHA1 of a commit to fetch a single commit. Let’s say for example we want to fetch only this commit from Apache Flink https://github.com/apache/flink/commit/c0cf91ce6fbe55f9f1df1fb1626a50e79cb9fc50
git clone --depth=1 https://github.com/apache/flink.git c0cf91ce6fbe55f9f1df1fb1626a50e79cb9fc50
If you compare with a full clone we downloaded only 22005 instead of 89581.