Git

What is Git sparse checkout

The Git repositories support has enabled the users to work parallelly; means multiple contributors can make changes to a single project. Git repositories are managed by GitHub; thus, making it suitable to use locally and with cloud support as well. It can handle a variety of project sizes varying from small leading to large and tracks the evolution of these projects from the date of creation. Sometimes, it is noticed that long-term projects usually take more space and have a mesh of directories. So, in this case, it is difficult to get the required content easily; Git sparse checkout option enables to populate the working directory with the required content and excluding the unnecessary files. However, this option does not impact the size of the repos; thus, size remains the same before and after applying Git sparse checkout. In this article, we will elaborate on the usage of Git sparse checkout to clone the required content on your local system.

Before digging the details, let’s have a look at the syntax of the Git sparse checkout:

git sparse-checkout <subcommands> [options]

The subcommands and options provide extensive support to this command, like initialization of Git sparse checkout file, writing patterns to this file, listing down the sparse directories, and many more. The general working mechanism of this useful command is described in the next section.

How Git sparse checkout works

Git sparse checkout works on skip-work tree reference bitmap, and a file $GIT_DIR/info/sparse-checkout is used to define the skip-work tree. Majorly, there are two steps that need to be followed to make git sparse checkout helpful: whenever the working directory is updated, the skip-work tree bits are updated in the file. Afterward, matching is performed, where the files matching the pattern will be kept, and other files are not loaded. So, this is how Git sparse checkout offers only those files that are requested by the user.

How to use Git sparse checkout

This core section contains the use and application of this command to the Git repository; we have broken down this into several steps:

Step 1: Update and Upgrade the system

It is recommended to update your system repository and upgrade the packages because the Git sparse checkout option may not be available on older versions of Git: So, use the commands stated below to perform the update and upgrade:

$ sudo apt update && sudo apt upgrade

As the git sparse-checkout command is supported on and after git versions “2.22” so before continuing, you must check the git version by using the command mentioned below

$ git --version

Step 2: Initialize the git repository and fetch the GitHub Project

Once you are done with the version confirmation; you have to initialize the git repository by using the command stated below: we have created a folder “sparse” in the “Home” directory and will initialize the git repository here:

$ git init

Text Description automatically generated

Once the repository is initialized, fetch the GitHub project by issuing the command mentioned below:

Note: you can add any GitHub project link that you want to use for the sparse-checkout command:

$ git remote add -f origin https://github.com/marcusanth/Bat-inspired-test-case-prioritization.git

Text Description automatically generated

Step 3: Update the config file and Initialize the git sparse-checkout

After fetching the GitHub project, you have to make a change to the config file and make its action true for sparse checkout by issuing the command written below:

$ git config core.sparseCheckout true

Once you are ready to sparse the directories, make sure your terminal is open in the respective Git project where you want to apply Git sparse checkout. Now, initialize the Git sparse checkout by using the command written below:

$ git sparse-checkout init

Step 4: Apply the sparse-checkout to fetch repositories

The image below shows the list of files in the GitHub project: here we will use sparse to get two repositories, “batAlgorithm” and “testCases”:

A screenshot of a computer Description automatically generated with medium confidence

Write and apply the command written below to do so:

$ git sparse-checkout set batAlgorithm testCases

Additionally, you must pull the master if you are using the sparse-checkout first time in the git repository:

$ git pull origin master

Text Description automatically generated

Step 5: Verify the change

The time you will pull the master branch; the selected directories will be available in the same directory where you have initialized the Git repository: you can use the command mentioned below to get the list of files available:

$ ls -al

Or:

You can list down the directories that are cloned; for this, copy and paste the command mentioned below:

It is noticed that names of both directories (“batAlgorithm” “testCases”) are displayed after executing this command:

$ git sparse-checkout list

Text Description automatically generated

How to disable Git sparse checkout in Ubuntu

If you are stuck somewhere while using the git sparse-checkout command, then you can disable this operation by using the command mentioned below:

$ git sparse-checkout disable

Once you disable the sparse-checkout, it will fetch all the files from the GitHub project, as can be seen in the image below:

Text Description automatically generated

Conclusion

Git repositories have emerged as one of the primary needs for the users that have to work on shared projects, and it supports several operations to ease the working on the same project. Git is usually adopted by programmers; human resource departments of large-scale organizations and they work collaboratively that sometimes turn into large repositories. In the case of big projects, it would be difficult for you to locate directories and work on them; for this, the Git sparse-checkout command helps you to do so. In this article, we have briefly described the usage of this command and provided a detailed guide that provides step by step demonstration of cloning the GitHub project files to the local Git repository.

About the author

Adnan Shabbir