Git

Is it Possible to do a Sparse Checkout Without Checking Out the Whole Repository First?

Git allows developers to work parallel on a large project where they can work together at the same time. Sometimes, they deal with a long-term project having multiple files that take more space and time to check out. So, it becomes hard for developers to get the desired content quickly. In this case, the Git sparse checkout feature can be used to get required content from the remote repository.

This article will discuss the method of sparse checkout without downloading the entire Git repository.

Do Git Users Can Sparse Checkout Without Downloading the Entire Git Repository First?

Yes, Git users can do a sparse checkout without checking out the entire Git repository. For this corresponding purpose, try the below-listed instructions:

  • Go to the desired local directory.
  • Set sparse checkout value.
  • Add a remote URL and verify it.
  • Apply sparse checkout to pull specific Git repositories using the “$ git pull <remote-name> <branch-name>”.
  • Verify new changes.

Step 1: Navigate to the Local Git Directory

Move to the particular local repository with the help of the “cd” command:

$ cd "C:\Git\Repo1"

Step 2: Check Default Spare Checkout Value

Then, execute the provided command to check the default value of “core.sparseCheckout” from the configuration file:

$ git config core.sparseCheckout

According to the below-listed output, the default value of sparse-checkout is “false”:

Step 3: Enable Sparse Checkout

To enable the sparse checkout, run the “git config” command along with the particular parameter “core.sparseCheckout” and its value “true”:

$ git config core.sparseCheckout true

Step 4: Verify Configuration Setting

To ensure whether the desired setting has changed or not, use the below-stated command:

$ git config core.sparseCheckout

It can be seen that the sparse checkout has been enabled:

Step 5: Copy Remote URL

After that, go to desired GitHub remote repository and copy its “HTTPS” URL:

Step 6: Add Remote “origin”

Now, add the remote URL into the local repository to build a connection between local and remote repositories by running the provided command:

$ git remote add -f origin https://github.com/laibayounas/demo.git

Here, the:

  • -f” flag represents the “fetch” to download the updated remote repository.
  • origin” is the remote URL name.
  • https://….” is the GitHub repository path.

After executing the above-stated command, the remote URL will add and fetch the updated remote repository content as well:

Step 7: Verify the Remote URL

Then, verify whether the remote origin has been added or not through the below-provided command:

$ git remote -v

Step 8: Apply Sparse Checkout to Fetch Specific Repository

Execute the “git sparse-checkout” command along with the desired repository or file name to fetch that specific repository/file:

$ git sparse-checkout set test_repo

Step 9: Pull the Repository

Next, download the content of the specific branch using the “$ git pull” command along with the remote name and desired branch:

$ git pull origin alpha

Step 10: Verify Changes

Finally, execute the “$ git sparse-checkout list” command to display the remote pulled content through sparse-checkout:

$ git sparse-checkout list

It can be observed that the only previously specified repository has fetched from the remote branch:

We have explained the procedure of sparse checkout without checking out the whole repository.

Conclusion

Yes, Git users can do a sparse checkout without downloading the entire Git repository. To do so, first, go to the required local Git repository. Enable the sparse checkout feature by utilizing the “$ git config core.sparseCheckout” command and specify its value as “true”. Then, add the remote URL and fetch the particular repository to the local Git repository simultaneously. Lastly, run the “git pull <remote-name> <branch-name>” command to pull the remote repository. This article provided the method of sparse checkout without checking out the entire Git repository first.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.