Git

Git Shallow Clone and Clone Depth

Using Git, developers can work independently on their local machine without depending on the GitHub hosting server. Developers deal with many source code project files with the help of branches. However, sometimes it becomes difficult to maintain the long history of the repository, especially when it is needed to clone the latest content of the remote repositories. At that time, you may encounter issues related to repository size, build server, or download times.

To overcome this problem, Git provided the shallow clone depth feature. Git shallow is used for specifying the depth, which means providing the desired number for cloning remote repository commits and saving a lot of disk space and time.

The outcomes of this blog are:

What is Git Repository Shallow Clone?

Developers may have large projects or have an uncountable number of commits. However, if they want to clone the desired commits rather than the entire repository, the Git shallow clone is the easiest way to do so. It enables developers to clone the most recent commit of their repository instead of cloning a whole Git repository.

What is Git Repository Clone Depth?

On Git, the shallow clone depth is the number of required commits developers want to download when they clone any repository.

How to Clone Git Remote Repository and Check Size?

To clone a Git remote repository without specifying the specific number of commits, follow the below-provided steps.

Step 1: Go to Required Repository

First, run the “cd” command along with the required repository path and navigate to it:

$ cd "C:\Users\nazma\Git\Test_13"

Step 2: Check Remote URL

Then, check the existing list of remote URLs by running the “git remote” command:

$ git remote -v

Step 3: Clone Desired Remote Repository

Next, execute the “git clone” command to clone the remote repository into the local machine:

$ git clone https://github.com/GitUser0422/demo.git

Step 4: View Repository List of Content

Now, check the current repository list of content by using the “ls“ command:

$ ls

Step 5: Check Cloned Repository Size

Lastly, run the following command to check the size of the clone remote repository:

$ du -sh demo/

In the above-stated command, the “du” stands for “disk usage” used to display the usages of disks for a particular directory.

As you can see in the below-provided output, the specified repository “demo/” has a “140k” size:

Note: The entire repository has been cloned into the local repository in the above-listed process. Now, move toward the next section, clone the repository with the desired depth and find the difference between them.

How to Shallow Clone Git Depth Remote Repository and Check Size?

To shallow clone a desired Git remote repository with depth, try out the below-listed examples.

Example 1: Shallow Clone Git Depth “1”

This example will demonstrate the procedure of Shallow Clone Depth with a “1” depth level.

Step 1: Git Shallow Clone

Execute the “git clone” command along with the required remote repository URL and depth:

$ git clone --depth 1 https://github.com/GitUser0422/demo.git

Here, the “–depth” is a parameter that contains the value “1” which means that we want to clone the most recent “1” commit of the specified remote repository:

Step 2: View Clone Repository Size

Now, run the “du -sk” command to display the size of the desired repository:

$ du -sh demo/

According to the below-given output, the shallow clone depth repository “demo/” has the “98k” size:

Step 3: Verify Shallow Clone Depth

Next, check the Git reference log history to ensure the shallow clone with depth “1” by running the “git log” command along with the “–oneline” option:

$ git log --oneline

It can be observed that only the most recent commit of the remote repository has been cloned:

Example 2: Shallow Clone Git Depth “3”

This example will demonstrate the procedure of Shallow Clone Depth with a “3” depth level.

Step 1: Shallow Clone Remote Repository

First, execute the below-given command along with the depth “3”:

$ git clone --depth 3 https://github.com/GitUser0422/demo.git

Here, the specified parameter “–depth” with the value “3” is used to clone the most recent three commits of the particular repository:

Step 2: View Repository Size

Then, show the cloned repository size by running the “-du -sh” command with the local copy repository name:

$ du -sh demo/

It can be seen that the cloned three most remote repository commits have the “98k” size.

Step 3: Move to Local Copy Repository

Next, execute the “cd” command and navigate to the specified repository:

$ cd demo/

Step 4: Check Git Reference Log History

Lastly, view the Git reference log history to verify the remote repository three commits:

$ git log --oneline

As observed, we have successfully cloned the most recent specific commit:

That’s all! We have effectively compiled the process of the Git shallow depth clone.

Conclusion

Git shallow clone depth is the number of required commits that developers want to download when they clone the desired repository. To do so, first, move to the particular repository and check the remote URL list. Then, run the “$ git clone –depth <depth-value> <remote-repo-url>” command. Lastly, check the size of the cloned repository and Git reference log history. This blog demonstrated the method of Git shallow depth clone.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.