Git

How to Pull Specific Directory With Git

GitHub remote server having multiple files and directories containing project source code. Developers can pull and push changes between the local and remote repositories. Moreover, they can pull a particular remote directory. For this purpose, they need to create the sparse checkout by modifying the Git config file with the help of the “git config core.sparsecheckout true” command.

This blog illustrated the process of pulling a specific directory with Git.

How to Pull Specific Directory With Git?

To pull a specific directory with Git, check the following procedure:

    • Go to the GitHub server and choose the particular directory which needs to be pulled.
    • Redirect to the desired Git repository.
    • List its repository content, including hidden files.
    • Create a sparse checkout with the help of the “git config core.sparsecheckout true” command.
    • Add the remote directory name to the sparse checkout property.
    • Utilize the “git pull <remote-name> <local-branch>” command.

Step 1: Select Remote Directory

First, open your favorite web browser, switch to the GitHub repository, and choose the particular directory. For instance, we have selected the “My_repo” directory:


Step 2: Redirect to Git Repository

Now, navigate to the Git repository by typing out the “cd” command:

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

 
Step 3: Initialize Repository

Next, execute the “git init” command to initialize the empty repository:

$ git init

 

Step 4: List Hidden Content

To list the current repository’s existing content including hidden, utilize the “ls -al” command:

$ ls -la

 

Step 5: List Remote URL

Then, execute the “git remote” command to check the existing remote URL:

$ git remote -v

 

Step 6: Set ‘core.sparsecheckout’ Value

To create the sparse checkout, set the value of the “core.sparsecheckout” to “true” through the Git configuration file:

$ git config core.sparsecheckout true

 
Here, sparse checkout will change the working tree from having all staged files exist to just having a subset of those files:


Step 7: Add Specific Directory to Sparse Checkout Property

In order to add the selected remote directory name to the sparse checkout property through the “echo” command, as shown below:

$ echo 'My_repo' >> .git/info/sparse-checkout

 

Step 8: Pull Remote Branch

Finally, execute the “git pull” command to download the latest version of the particular remote branch:

$ git pull origin master

 

Step 9: Verify Pull Directory

Lastly, view the list of the current working repository existing content to ensure that the desired remote directory is pulled or not:

$ ls -la

 
According to the below-stated output, the “My_repo” directory is pulled successfully:


That’s all! We have compiled the method of pulling specific directories with Git.

Conclusion

To pull a specific directory with Git, first, go to the GitHub server and choose the particular directory which you want to pull. Then, move to the Git desired repository, and list its content, including hidden content. Next, create a sparse checkout through the “git config core.sparsecheckout” by setting its value to “true”. After that, add the remote directory name to the sparse checkout property and execute the “git pull <remote-name> <local-branch>” command. This blog illustrated the process of pulling specific directories with Git.

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.