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?

- 25,879
- 13
- 79
- 119

- 10,220
- 11
- 43
- 56
-
3sparse checkout might interest you (even though you still need to fetch everything): see http://stackoverflow.com/questions/2416815/how-to-git-pull-all-but-one-folder/2416991#2416991 – VonC Mar 11 '10 at 14:21
-
"Clone" and "pull" are two different operations in git. The question asks about both, and you've gotten some answers for each one, but it's not always clear which one each answer is intended to address. Which is confusing. – LarsH Apr 21 '22 at 14:21
-
If you're interested in GitHub specifically - please see this answer: https://stackoverflow.com/a/38879691/1027842 – Benjamin Apr 03 '23 at 21:13
10 Answers
- cd into the top of your repo copy
git fetch
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.

- 3
- 2

- 2,686
- 1
- 18
- 10
-
2Just realized I misunderstood the question. My reply instead addresses how to checkout a single file, which can then be modified and committed. – vergueishon Feb 04 '11 at 20:20
-
You addressed the title of the question, which is good enough. This just saved me, so as far as I'm concerned, this is the correct answer - especially since this was a top hit on Google for this issue. – Morgon Nov 01 '11 at 14:39
-
30This still gets the whole repo though. For a project 2GB in size, this doesn't save much time.. – Pithikos Oct 24 '14 at 18:09
-
9For those that read these comments and don't know how this doesn't answer the question, what this solution does is give you a copy of only that one folder, but it's not a working copy, meaning you can't modify and re-commit. So as @Morgon said, it addresses the title of the question (you CAN pull only one dir), but doesn't answer the body of the question ("make some changes, commit and push back again"). – msb May 10 '17 at 20:52
-
@StanHolodnak `--` is not required though. It forces the bits after it to be interpreted as a path. So, yes, if you have a file with the name `--help` that you want to checkout, prefixing it with these hyphens would be required: `git checkout HEAD -- --help` – Tim Visée Dec 01 '17 at 10:07
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
-
this works if you only want to use remote version of a file to replace local file, i think – John Jan 16 '18 at 10:42
-
this pull the folder also, how to not donwload the folder, but only things inside the folder? – Al Kasih Jul 18 '18 at 05:09
-
There isn't any need to `fetch` or `checkout` commands, we can simply make the pull request after `remote add` command to get the whole master branch. – Iqra. Nov 15 '18 at 11:09
-
11As Pithkos points out in the accepted answer, this answer also will download the WHOLE remote repository to your local machine. shingara's answer is the most clear - there is no way to remotely pull a portion of a repo. However, for those who don't mind the bandwidth of the WHOLE fetch, and are interested in making a working copy of a portion of the local cache, then the accepted answer shows you how. But I just want to make it clear the the entire history of the repo (in a compressed format) must be downloaded to your machine. – Gabe Halsmer Oct 17 '19 at 17:44
-
Most answers/techniques download the whole repository, even if you will only see/use a subset of it. I don't like that, as some things I work on have a lot of big files that don't interest me. 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
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. This solution will only download what you want, nothing more.
Many many thanks to @cforbish !

- 3,899
- 3
- 31
- 38
-
8I'm posting it here mostly because whenever I try to google how to do that, this is a top result, and that thread is not even close to being displayed. :( I feel bad copy-pasting, but I reckon it's SO best practices? – msb May 10 '17 at 20:48
-
5This is the only one clean solution. Tried the answers above. After I checked out my branch with the other answers, I couldn't pull, and had several new files that needed committing. With this solution everything looks right. But NOTE: Regardless of the branch that was checked out, git will think you are in `master`. Just make sure you do `git branch --set-upstream-to=origin/
` to pull correctly – seebiscuit Jun 25 '18 at 14:24 -
1Had to do `git read-tree -m -u HEAD`(as illustrated [here](http://vmiklos.hu/blog/sparse-checkout-example-in-git-1-7)) because branch was different. – TheMaster Feb 23 '23 at 04:17
If you want to get the latest changes in a directory without entering it, you can do:
$ git -C <Path to directory> pull

- 819
- 8
- 10
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à"

- 5,361
- 3
- 34
- 63

- 195
- 1
- 2
-
3This downloads a specific directory of file for archiving purposes, this doesn't pull a copy you can modify and push back. – leetNightshade Feb 02 '17 at 20:41
-
4Very cool. Probably not acceptable as solution to the OP but it's actually what *I* was after since I just need to search the contents of a big folder in an even bigger repo I definitely don't want to fetch, and I don't need to modify/push. – Oliver Apr 01 '20 at 20:31
-
As @leetNightshade said, this is not the right way to answer this. – Arka Mukherjee May 30 '22 at 21:28
git clone --filter
+ git sparse-checkout
downloads only the required files
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 subdirectory small/
of this repository: https://github.com/cirosantilli/test-git-partial-clone-big-small-no-bigtree I can do:
git clone -n --depth=1 --filter=tree:0 \
https://github.com/cirosantilli/test-git-partial-clone-big-small-no-bigtree
cd test-git-partial-clone-big-small-no-bigtree
git sparse-checkout set --no-cone small
git checkout
I have covered this in more detail at: How do I clone a subdirectory only of a Git repository?
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.
Tested on git 2.30.0 on January 2021.

- 347,512
- 102
- 1,199
- 985
It's not possible. You need pull all repository or nothing.

- 46,608
- 11
- 99
- 105
-
1Even though you need to *fetch* everything, would a pull on a sparse checkout working tree be of interest in this case? See http://stackoverflow.com/questions/2416815/how-to-git-pull-all-but-one-folder/2416991#2416991 – VonC Mar 11 '10 at 14:22
-
11Just to add on this, the reason why you cannot pull just a directory is because git uses data semantic tracking, not file semantic tracking, so you can seamlessly move code (or other data) in and out of files without having to tell the source tracking system (until you update of course.) Because of this, code can move in and out of directories seamlessly too, so grabbing just one directory doesn't make as much sense. Hope this helps. – OmnipotentEntity Oct 29 '10 at 04:38
-
14@OmnipotentEntity Sorry but IMHO it _does_ make sense. The implementation shouldn't limit the use. Git does know what directories are (at least when creating a local working copy) so there's no reason it couldn't perform some similar operation on the server and just send the relevant output. Granted, that's not currently implemented but that's due to a lack of a feature, not a fundamental impossibility. Personally I think it's a pretty important feature but hey, that's one reason I avoid git when possible. – Basic Feb 24 '15 at 12:59
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!

- 160
- 1
- 3
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

- 300
- 5
- 16
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.

- 146,289
- 39
- 203
- 257