Git is a distributed version control system used by many users around the world. However, it requires every developer to clone the entire repository before they start working on it. It can be very time consuming to download an entire repository with all its history and files. So git provides a convenient way called shallow clone, that lets you download only the recent commits, instead of the entire repository. This allows you to quickly clone a git repository and start contributing to it. In this article, we will look at how to use git shallow clone.
What is Git Shallow Clone
Git Shallow clone allows you to clone only the most recent commits of your repository, instead of cloning the entire repository. If your project has many years of history, or plenty of commits, then you can use git shallow clone to pull commits upto a particular clone depth.
Also read : How to Install Git in Ubuntu
What is Git Shallow Clone Depth
Shallow clone depth is the number of most recent commits that you want to download when you clone a repository.
Also read : How to Redirect Subdirectory to Root in NGINX
How To Use Git Shallow Clone
It is very easy to do a git shallow clone. Here is its syntax.
git clone -–depth [depth] [remote-url]
Let us say you have a remote git repository demo_repository at https://github.com/test_user/demo_repository, and you want to clone it with just latest 10 commits included in it. Here is the git clone command to do so.
$ sudo git clone --depth 10 https://github.com/test_user/demo_repository
It is also possible to shallow clone a specific branch instead of the entire repository. Here is the command to do git shallow clone of a single branch
git clone [remote-url] --branch [name] --single-branch [folder]
Also read : How to Install Pip in Ubuntu
How to clone a large repository
When you try to clone a large remote repository, it may time out and give an error. In such cases, you can also use git shallow clone to completely download a large repository. Here are the steps to do it.
Let us say you want to fully clone a large repository https://github.com/test_user/demo_repository.
First, you do a git shallow clone of your repository with clone depth=1
$ sudo git clone --depth 1 https://github.com/test_user/demo_repository
The above command will quickly download and create a local clone of your remote repository with the last 1 commit.
Also read : How to Enable Gzip in NGINX
Next, run the following command to download rest of the commits.
$ sudo git fetch --unshallow
Sometimes, your remote repository may be still too large for a full clone. In such cases, do a shallow clone again to fetch some more recent commits. For example, shallow clone last 25 commits.
$ sudo git clone --depth 25 https://github.com/test_user/demo_repository
Then run git fetch –unshallow command to download the rest of the commits.
Git shallow clone can save time while cloning large repositories. However, if you push commit from a shallow clone it can take more time due to the difference calculations to be done between remote and shallow copies of your repository. So it is advisable to start with a shallow clone but eventually do a full clone.