Pull a specific folder in Git
This article explains how we can extract a specific folder from a remote Git repository. This approach comes in handy when we want to work on certain files in a large repository.
Downloading a single folder from a 2GB repository can save a lot of time. Without further ado, let's jump right in.
Pull a specific folder in Git
The simplest method we can use to extract a specific folder involves using Git Sparse Checkout
the .exe function. We could explain the concept, but an example will work for this situation.
In the example below, we have a remote repository hosted on GitHub that contains a number of files arranged in directories. We want to pull and work on the application folder.
We will follow the steps below to pull the application folder.
On Git Bash, we will create a directory to store our application folder.
$ mkdir Djangp-datta-able
Use the cd command to navigate to your directory and initialize the Git repository.
$ git init
This is the current content 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 that we want to pull from to our local repository. We will run:
$ git remote add origin https://github.com/app-generator/django-datta-able.git
Once that is done, we can go ahead and enable the sparse checkout property in our repo. We will use git config
the command as shown below.
$ git config core.sparsecheckout true
We can then add the application folder to the sparse checkout properties as shown below.
$ echo 'apps' >> .git/info/sparse-checkout
You can replace app with the directory you want to extract from. You can also add multiple directories to the sparse checkout feature.
All that's left is to pull from the remote repository. We will run:
$ git pull origin master
The above command will take the application folder and clone it to our local directory. Let’s check the contents of our local repository.
$ 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 an apps/ folder. When inspecting our directory we find:
There you have it. We have successfully pulled a folder from the remote repository.
We can now modify the code, commit and push it back to the remote repository.
Before we wrap up, there is another approach we can use, which we will discuss shortly. However, the approach we are about to discuss will not allow us to modify the code and push it back to the remote.
After adding the remote repository to your local repository, run git fetch
the command and view the folder of interest as shown below.
$ git checkout HEAD path/to/your/dir/or/file
We got the above method from Stack Overflow, which only shows that it can pull a specific folder from a remote repository. If you want to modify, commit, and push back to the remote, use the sparse checkout feature.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git shows remote tracking branches
Publish Date:2025/04/03 Views:125 Category:Git
-
Branches on remote Git repositories are called remote branches. These are pointers to our remote repositories, including branches, tags, etc. Local branches only exist on each developer's local personal computer, but there is only one remot
View merged and unmerged branches in Git
Publish Date:2025/04/03 Views:96 Category:Git
-
This article discusses how to list merged and unmerged branches in Git. Git branches encourage convergent evolution of code. This is where we create a branch as a temporary space to work on a feature, and then merge the branch with its orig
The difference between Fork and Branch on GitHub
Publish Date:2025/04/03 Views:157 Category:Git
-
This article discusses the difference between Form and Branch on GitHub. In the context of coding, the current era relies more on collaboration. GitHub is one of the most commonly used collaboration tools. Forking and branching on GitHub ar
How to determine the current branch in Git
Publish Date:2025/04/03 Views:164 Category:Git
-
Git is a unique and popular version control system that is used by most of the software developers to keep an eye on the changes made in various applications and stay connected with other teams on the running projects. It helps large teams
Difference between Git Merge Origin/Master and Git Pull
Publish Date:2025/04/03 Views:195 Category:Git
-
This article outlines the differences between the git merge origin/master and git pull commands. These two commands integrate changes from a remote repository into the current local branch. However, each command's operation is unique and ha
Git Pull Origin branch overwrites Master branch
Publish Date:2025/04/03 Views:143 Category:Git
-
This article explains how we can revert the changes made to the master git pull origin branch branch after running the command . Assume that you have a master branch and a feature branch in your local and remote repositories . You pull chan
Git Pull and Merge Commands
Publish Date:2025/04/03 Views:56 Category:Git
-
In the vast world of version control systems, Git is the only fast, well-organized, in-demand, and easily accessible version control system that enables users to work on various projects simultaneously in an efficient manner without any col
Difference between Git Checkout --Track Origin/Branch and Git Checkout -B Branch
Publish Date:2025/04/03 Views:185 Category:Git
-
This article outlines the difference between the git checkout -b branch origin/branch and git checkout --track origin/branch commands. We use these two commands to check out remote branches in a Git repository. These two commands have the s
Rolling back to an old commit in a public Git repository
Publish Date:2025/04/03 Views:115 Category:Git
-
This article explains how we can roll back a public Git repository to an old commit. When using Git version control, we can go back to any desired point. Without further ado, let’s get into today’s agenda. Rolling back to an old commit