How to pull specific directory with git

GitDirectoryPull

Git Problem Overview


I have a project with git, and I just want to clone or pull a specific directory, like myproject/javascript just like subversion does.
make some changes, commit and push back again.
It's possible?

Git Solutions


Solution 1 - Git

  1. cd into the top of your repo copy
  2. git fetch
  3. git checkout HEAD path/to/your/dir/or/file
  • Where "path/..." in (3) starts at the directory just below the repo root containing your ".../file"

  • NOTE that instead of "HEAD", the hash code of a specific commit may be used, and then you will get the revision (file) or revisions (dir) specific to that commit.

Solution 2 - Git

In an empty directory:

git init
git remote add [REMOTE_NAME] [GIT_URL]
git fetch REMOTE_NAME
git checkout REMOTE_NAME/BRANCH -- path/to/directory

Solution 3 - Git

After much looking for an answer, not finding, giving up, trying again and so on, I finally found a solution to this in another SO thread:

[How to git-pull all but one folder][1]

To copy-paste what's there:

git init
git remote add -f origin <url>
git config core.sparsecheckout true
echo <dir1>/ >> .git/info/sparse-checkout
echo <dir2>/ >> .git/info/sparse-checkout
echo <dir3>/ >> .git/info/sparse-checkout
git pull origin master

To do what OP wants (work on only one dir), just add that one dir to .git/info/sparse-checkout, when doing the steps above.

Many many thanks to @cforbish !

[1]: https://stackoverflow.com/a/17075665/2193235 "How to git-pull all but one folder"

Solution 4 - Git

If you want to get the latest changes in a directory without entering it, you can do:

$ git -C <Path to directory> pull

Solution 5 - Git

Maybe this command can be helpful :

git archive --remote=MyRemoteGitRepo --format=tar BranchName_or_commit  path/to/your/dir/or/file > files.tar

"Et voilà"

Solution 6 - Git

It's not possible. You need pull all repository or nothing.

Solution 7 - Git

git clone --filter from git 2.19 now works on GitHub (tested 2020-09-18, git 2.25.1)

I'm not sure about pull/fetch, but at least for the initial clone, this option was added together with an update to the remote protocol, and it truly prevents objects from being downloaded from the server.

E.g., to clone only objects required for d1 of this repository: https://github.com/cirosantilli/test-git-partial-clone I can do:

git clone \
  --depth 1 \
  --filter=blob:none \
  --no-checkout \
  https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git checkout master -- d1

I have covered this in more detail at: https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository/52269934#52269934

It is very likely that whatever gets implemented for git clone in that area will also have git pull analogues, but I couldn't find it very easily yet.

Solution 8 - Git

Tried and tested this works !

mkdir <directory name> ;  //Same directory name as the one you want to pull
cd <directory name>;
git remote add origin <GIT_URL>;
git checkout -b '<branch name>';
git config core.sparsecheckout true;
echo <directory name>/ >> .git/info/sparse-checkout;
git pull origin <pull branch name>

Hope this was helpful!

Solution 9 - Git

For all that struggle with theoretical file paths and examples like I did, here a real world example: Microsoft offers their docs and examples on git hub, unfortunately they do gather all their example files for a large amount of topics in this repository:

https://github.com/microsoftarchive/msdn-code-gallery-community-s-z

I only was interested in the Microsoft Dynamics js files in the path

msdn-code-gallery-community-s-z/Sdk.Soap.js/

so I did the following

create a

msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout

file in my repositories folder on the disk

git sparse-checkout init

in that directory using cmd on windows

The file contents of

msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout

is

Sdk.Soap.js/*

finally do a

git pull origin master

Solution 10 - Git

Sometimes, you just want to have a look at previous copies of files without the rigmarole of going through the diffs.

In such a case, it's just as easy to make a clone of a repository and checkout the specific commit that you are interested in and have a look at the subdirectory in that cloned repository. Because everything is local you can just delete this clone when you are done.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbluefootView Question on Stackoverflow
Solution 1 - GitvergueishonView Answer on Stackoverflow
Solution 2 - GitscottalanView Answer on Stackoverflow
Solution 3 - GitmsbView Answer on Stackoverflow
Solution 4 - GitferalamilloView Answer on Stackoverflow
Solution 5 - Gitfabrice belleView Answer on Stackoverflow
Solution 6 - GitshingaraView Answer on Stackoverflow
Solution 7 - GitCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 8 - GitNnaikView Answer on Stackoverflow
Solution 9 - GitD MView Answer on Stackoverflow
Solution 10 - GitAbizernView Answer on Stackoverflow