Git is a versatile, free, and open-source version control system that helps keep track of changes made to project files. It’s like a helpful system for organizing and managing project work, making it easier for teams and individuals to work together smoothly. With Git, people can carefully watch for changes to project files, making sure everyone knows what’s happening and making it easier to work together on software projects.
However, sometimes, using Git can become difficult, especially with large repositories or monorepos. We will have to check out the entire repository, which is time-consuming and inefficient. This is especially problematic when you only need a small part, as it leads to slower performance and increased storage usage.
Sparse checkout is a feature in Git that allows us to checkout only a subset of files or directories from a repository, rather than the entire contents. This can be useful in situations where we have a large repository with many files, but we only need to work with or view a specific subset of files.
Sparse checkout is really useful in a few key situations:
Large repositories: If we are working with repositories having large files or a ton of files, checking out just the parts you need can make things run faster.
Monorepos: In repositories that contain multiple projects, we can use the sparse checkout feature to work on only one specific project.
Selective work: Sometimes, we only need to make changes in a specific directory or file. Sparse checkout lets us focus on just that part.
Here are the basic steps to perform a sparse checkout in Git:
First, we create a project directory MyProject
and navigate into it using the following commands:
mkdir MyProjectcd MyProject
Then, we initialize a new Git repository using the git init
command:
git init
Now, we create two files (file1.txt
and file2.txt
) and commit them:
touch file1.txtgit add file1.txtgit commit -m "My commit describing the file1.txt"touch file2.txtgit add file2.txtgit commit -m "My commit describing the file2.txt"
Now, we create a folder MyFolder
, navigate into it, create and commit file3.txt
within the folder and then navigate back one level using the cd ..
command:
mkdir MyFoldercd MyFoldertouch file3.txtgit add file3.txtgit commit -m "My commit describing the file3.txt"cd ..
Now, we confirm the contents of the current directory using the ls
command:
ls
To enable sparse checkout, we first initialize it in our repository:
git config core.sparseCheckout true
Then, we can define which files or directories we want to include in our sparse checkout by specifying their paths in a file named .git/info/sparse-checkout
. For example:
echo "file1.txt" >> .git/info/sparse-checkoutecho "MyFolder/*" >> .git/info/sparse-checkout
After setting up the sparse checkout, we can update our working directory to include only the specified files and directories:
git read-tree -mu HEAD
This command reads the tree information from the current commit (HEAD
) and updates the working directory accordingly. This ensures that only the files and directories specified in the sparse checkout configuration are included.
Finally, check out the files specified in the sparse-checkout
file:
git checkout
After performing these steps, Git will only populate our working directory with the files and directories specified in the sparse-checkout
configuration, rather than the entire repository.
Kindly click "Click to Connect." We have run all the commands for you in the terminal below. Please observe the output.
Free Resources