How to Pull a Specific Folder in Git

John Wachira Feb 02, 2024
  1. Use Sparse Checkout to Pull a Specific Folder in Git
  2. Use Submodules to Pull a Specific Folder in Git
  3. Use Git Worktree to Pull a Specific Folder in Git
  4. Conclusion
How to Pull a Specific Folder in Git

Git is a powerful version control system that allows developers to manage and collaborate on projects efficiently. One common task in Git is pulling changes from a remote repository.

While you can pull all changes using git pull, sometimes you may want to pull only a specific folder. This can be particularly useful when you’re working on a large project with multiple directories, but you only need updates for a specific part of it.

In this guide, we’ll walk you through the methods to pull a specific folder from a Git repository.

Use Sparse Checkout to Pull a Specific Folder in Git

The easiest route we can use to pull a specific folder involves the use of the Git Sparse Checkout feature. Sparse checkout allows you to work with only a portion of a Git repository; it’s an efficient way to pull specific folders.

In the example below, we have a remote repository hosted on GitHub with dozens of files arranged in directories. We want to pull and work on the apps folder.

remote repository

Prerequisites

Before we begin, make sure you have the following:

  • Git installed on your local machine.
  • Access to the Git repository you want to work with.
  • Basic knowledge of Git commands.

We will follow the steps below to pull the apps folder.

  • On Git Bash, we will create a directory to store our apps folder.
    $ mkdir Django-datta-able
    
  • $ git init
    

    Here are the current contents of our local repository:

    $ ls -la
    total 4
    drwxr-xr-x 1 pc pc 0 Aug 31 09:45 ./
    drwxr-xr-x 1 pc pc 0 Aug 31 09:44 ../
    drwxr-xr-x 1 pc pc 0 Aug 31 09:45 .git/
    
  • Next, we will add the remote repository we want to pull from to our local repo. We will run:
    $ git remote add origin https://github.com/app-generator/django-datta-able.git
    
  • Once that is done, we can proceed to enable the sparse checkout properties in our repo. We will use the git config command, as shown below.
    $ git config core.sparsecheckout true
    
  • We can then add the apps folder to the sparse checkout property, as shown below.
    $ echo 'apps' >> .git/info/sparse-checkout
    

    You can replace apps with the directory you want to pull from. You can also add multiple directories to the sparse checkout feature.

  • What’s left is to pull from the remote repository. We will run:
    $ git pull origin master
    

    The command above will fetch the apps folder and clone it to our local directory. Let’s check the contents of our local repo again.

    $ ls -la
    total 8
    drwxr-xr-x 1 pc pc 0 Aug 31 09:49 ./
    drwxr-xr-x 1 pc pc 0 Aug 31 09:44 ../
    drwxr-xr-x 1 pc pc 0 Aug 31 09:50 .git/
    drwxr-xr-x 1 pc pc 0 Aug 31 09:49 apps/
    

    You can see that our local repository now has the apps/ folder. On checking our directory, we will find:

    apps folder in directory

There you have it; we have successfully pulled a single folder from the remote repository.

We can now modify the code, commit, and push it back to the remote repository.

Before we sign off, there is another method we can employ, which we will discuss shortly. However, the method we are about to discuss will not allow us to modify the code and push it back to the remote.

After adding the remote repo to your local repository, run the git fetch command and check out the folder of interest, as shown below.

$ git checkout HEAD path/to/your/dir/or/file

We obtained the method above from Stack Overflow, and it only shows that it can pull a specific folder from a remote repository. Use the sparse checkout feature if you want to modify, commit, and push back to the remote.

Use Submodules to Pull a Specific Folder in Git

Git submodules allow you to include another Git repository as a subdirectory within your repository. This is valuable for managing dependencies or incorporating external code into your project.

Prerequisites

Before you start, ensure you have the following:

  • Git installed on your local machine.
  • Access to the Git repository where you want to add a submodule.
  • Basic knowledge of Git commands.

Adding a Submodule

To add a submodule, navigate to your local repository and run the following command:

git submodule add <repository_url> <subdirectory_path>

Replace <repository_url> with the URL of the repository you want to include and <subdirectory_path> with the path where you want the submodule to reside.

Once the submodule is added, you can navigate to the specific folder within it. Use the cd command to move into the submodule directory.

cd <subdirectory_path>

Now, you are within the submodule’s directory and can interact with it as needed.

Updating the Submodule

To pull the latest changes from the submodule’s repository, use the following commands:

git checkout <branch_name>
git pull

Replace <branch_name> with the branch you want to update.

Removing a Submodule (Optional)

If you no longer need the submodule, you can remove it from your repository. First, unregister the submodule:

git submodule deinit -f -- <submodule_path>

Then, remove the submodule from the repository:

rm -rf .git/modules/<submodule_path>
git rm -f <submodule_path>

Use Git Worktree to Pull a Specific Folder in Git

Git Worktree allows you to maintain multiple working copies of the same repository on your local machine. Each worktree has its branch, allowing you to make changes independently. This is particularly valuable when you need to work on different aspects of a project simultaneously.

Prerequisites

Before you begin, ensure you have the following:

  • Git installed on your local machine.
  • Access to the Git repository you want to work with.
  • Basic knowledge of Git commands.

Creating a New Worktree

To create a new worktree, open your terminal and navigate to the local repository.

cd <repository_directory>

Now, use the following command to create a new worktree:

git worktree add <path_to_worktree_directory> <branch_name>

Replace <path_to_worktree_directory> with the desired path for the new worktree and <branch_name> with the branch you want to work on.

Navigate to the newly created worktree directory:

cd <path_to_worktree_directory>

You are now in a separate working directory linked to the specified branch.

Making Changes and Committing

Within this worktree, you can navigate to the specific folder you’re interested in and make any necessary changes. Once you’ve made your changes, commit them as you would in a regular Git workflow.

git add .
git commit -m "Commit message"

Replace "Commit message" with a descriptive message for your changes.

Pulling Updates

If there are updates in the remote repository that you want to pull into your specific folder, navigate back to the original repository and perform a regular git pull operation.

cd <repository_directory>
git pull origin <branch_name>

This will update the entire repository, but your specific folder changes will be preserved.

Conclusion

In this comprehensive guide, we explored various methods to pull a specific folder from a Git repository. Each method has its strengths and use cases, allowing you to choose the one that best fits your workflow.

  • Sparse Checkout is an efficient way to selectively pull specific folders, allowing you to work with only the files you need.
  • Submodules are valuable for managing dependencies or incorporating external code into your project, making it easier to pull specific folders from other repositories.
  • Git Worktree provides a powerful approach to maintaining multiple working copies, enabling you to work on different aspects of a project simultaneously.

By mastering these techniques, you can streamline your Git workflow and work more efficiently on projects of any size. Remember to choose the method that aligns with your specific requirements and project structure.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Pull