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 installation and establishment of local warehouse service
Publish Date:2025/04/05 Views:89 Category:Git
-
Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper
git remote operation——multiple remote repositories for one project
Publish Date:2025/04/05 Views:131 Category:Git
-
Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓
Git cherry pick command usage
Publish Date:2025/04/05 Views:190 Category:Git
-
git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an
Comparison between Git merge and Git rebase
Publish Date:2025/04/05 Views:171 Category:Git
-
The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th
How to fix Git error Error: src refspec master does not match any
Publish Date:2025/04/05 Views:124 Category:Git
-
When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to
Rebase local branch when pulling changes from remote repository branch in Git
Publish Date:2025/04/05 Views:144 Category:Git
-
This article will cover the basics of rebasing your local branch when pulling changes from a remote repository branch in Git. We use the version control system Git to track changes made to files. We commit changes in a local branch in our l
Undo Git Stash
Publish Date:2025/04/04 Views:187 Category:Git
-
This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash
View a list of cache entries in Git
Publish Date:2025/04/04 Views:59 Category:Git
-
We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T
Git stores specific files
Publish Date:2025/04/04 Views:115 Category:Git
-
This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f