Before digging the details, let’s have a look at the syntax of the Git sparse checkout:
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:
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
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:
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:
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:
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:
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”:
Write and apply the command written below to do so:
Additionally, you must pull the master if you are using the sparse-checkout first time in the git repository:
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:
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:
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:
Once you disable the sparse-checkout, it will fetch all the files from the GitHub project, as can be seen in the image below:
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.