1972

I have my Git repository which, at the root, has two sub directories:

/finisht
/static

When this was in SVN, /finisht was checked out in one place, while /static was checked out elsewhere, like so:

svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static

Is there a way to do this with Git?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Nick Sergeant
  • 35,843
  • 12
  • 36
  • 44
  • 18
    possible duplicate of [Checkout subdirectories in Git?](http://stackoverflow.com/questions/180052/checkout-subdirectories-in-git) – Joachim Breitner Jul 04 '13 at 08:43
  • 2
    For a 2014's user, what the `git clone` simplest command?? I used [this **simple answer**](http://stackoverflow.com/a/2466755/287948). If there are something more simple, please comment – Peter Krauss Nov 01 '14 at 12:00
  • 1
    For those trying to clone the contents of the repository (not creating the root folder), this is a very easy solution: http://stackoverflow.com/questions/6224626/github-clone-contents-of-a-repo-without-folder-itself – Marc Mar 29 '15 at 12:38
  • 1
    @JoachimBreitner: That question is about *checking out* subdirectories in Git (which is easy), whereas this question is about *cloning* subdirectories in Git (which is impossible). – Jörg W Mittag Aug 31 '18 at 14:48
  • 2
    @NickSergeant: As of Git 2.19, released 3 weeks ago, this is finally possible, as can be seen in this answer: https://stackoverflow.com/a/52269934/2988 Consider accepting that one now. Note: in Git 2.19, only client-side support is implemented, server-side support is still missing, so it only works when cloning local repositories. Also note that large Git hosters, e.g. GitHub don't actually use the Git server, they use their own implementation, so even if support shows up in the Git server, it does not automatically mean that it works on Git hosters. (OTOH, they could implement it faster.) – Jörg W Mittag Nov 04 '18 at 09:22
  • 1
    **See also:** https://stackoverflow.com/questions/466303/git-branches-with-completely-different-content – dreftymac Nov 14 '19 at 17:23
  • 1
    I've created a ``bash`` function which avoids downloading the history, which retrieves a single branch and which retrieves a list of files or directories you need. See it here: https://stackoverflow.com/questions/60190759/how-do-i-clone-fetch-or-sparse-checkout-a-single-directory-or-a-list-of-directo – Richard Gomes Feb 12 '20 at 14:40
  • 4
    If you want to download a folder from a GitHub repo, https://download-directory.github.io/ might be just the thing – jemand771 May 04 '21 at 06:40
  • svn rocks. I use this feature for sub and nested repos. git cant do this. – JonathanC Oct 13 '21 at 10:11
  • Does this answer your question? [Is it possible to do a sparse checkout without checking out the whole repository first?](https://stackoverflow.com/questions/4114887/is-it-possible-to-do-a-sparse-checkout-without-checking-out-the-whole-repository) – not2savvy Nov 17 '22 at 13:34

31 Answers31

1825

What you are trying to do is called a sparse checkout, and that feature was added in Git 1.7.0 (Feb. 2012). The steps to do a sparse clone are as follows:

mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>

This creates an empty repository with your remote, and fetches all objects but doesn't check them out. Then do:

git config core.sparseCheckout true

Now you need to define which files/folders you want to actually check out. This is done by listing them in .git/info/sparse-checkout, eg:

echo "some/dir/" >> .git/info/sparse-checkout
echo "another/sub/tree" >> .git/info/sparse-checkout

Last but not least, update your empty repo with the state from the remote:

git pull origin master

You will now have files "checked out" for some/dir and another/sub/tree on your file system (with those paths still), and no other paths present.

You might want to have a look at the extended tutorial and you should probably read the official documentation for sparse checkout and read-tree.

As a function:

function git_sparse_clone() (
  rurl="$1" localdir="$2" && shift 2

  mkdir -p "$localdir"
  cd "$localdir"

  git init
  git remote add -f origin "$rurl"

  git config core.sparseCheckout true

  # Loops over remaining args
  for i; do
    echo "$i" >> .git/info/sparse-checkout
  done

  git pull origin master
)

Usage:

git_sparse_clone "http://github.com/tj/n" "./local/location" "/bin"

Note that this will still download the whole repository from the server – only the checkout is reduced in size. At the moment it is not possible to clone only a single directory. But if you don't need the history of the repository, you can at least save on bandwidth by creating a shallow clone. See udondan's answer below for information on how to combine shallow clone and sparse checkout.


As of Git 2.25.0 (Jan 2020) an experimental sparse-checkout command is added in Git:

git sparse-checkout init
# same as:
# git config core.sparseCheckout true

git sparse-checkout set "A/B"
# same as:
# echo "A/B" >> .git/info/sparse-checkout

git sparse-checkout list
# same as:
# cat .git/info/sparse-checkout
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Chronial
  • 66,706
  • 14
  • 93
  • 99
  • 1
    FYI: if above is not working on windows, make sure you use something like this: `echo some/dir/*>> .git/info/sparse-checkout`, note it seems need to use `/*` and there is no space after it before the `>>`. – wangzq Jan 08 '13 at 00:09
  • 1
    This question is probably a duplicate of http://stackoverflow.com/questions/4114887/is-it-possible-to-do-a-sparse-checkout-without-checking-out-the-whole-repository . I'm commenting on this one because the last command generates the following error: "Sparse checkout leaves no entry on working directory". Instead, just checkout the desired branch (e.g, master) using: git checkout master. – metator Jan 25 '13 at 02:13
  • 19
    on Apple the '-f' perimeter does not work. just do git remote add origin without -f – Anno2001 Feb 17 '13 at 10:58
  • 162
    It is an improvement but still needs to download and store a full copy of the remote repository in origin, which one might like to avoid at all if he is interested only in portions of the codebase (or if there is documentation subfolders as in my case) – a1an Jun 13 '13 at 12:42
  • @a1an: in my case it did not make the full copy within ``, but maybe it is because I used a local repo on the disk instead of a remote repo. – basZero Aug 07 '13 at 15:34
  • 64
    Is there a way to clone desired directory contents (not directory itself) right into my repository? For example I want clone contents of `https://github.com/Umkus/nginx-boilerplate/tree/master/src` right into `/etc/nginx` – mac Apr 10 '14 at 05:40
  • 4
    Note that this still copies all the code locally, because adding a remote is an implied `fetch`. – ErikE May 14 '14 at 06:38
  • @ErikE That’s not quite correct. Adding a remote is not an implied fetch. The pull is the command that triggers a fetch. But the whole history is indeed downloaded there, as a1an already noted. Only the checkout is sparse. – Chronial May 14 '14 at 09:23
  • @Chronial I'm sorry, I missed that a1an already said the same thing. But I think you misunderstand fetch. Last night I ran `git remote add -f origin ` as given in this post, and git downloaded the entire 1.62GB repository. And there are 1.62GB of files in the .git folder. And `git branch -a` shows `/remotes/origin/branch`. It is **exactly** correct. Adding a remote is an implied, initial, `fetch`. – ErikE May 14 '14 at 18:26
  • 27
    @Chronial, @ErikE: you're both right / wrong :P The `git remote add` command does *not* imply a fetch, but `git remote add -f`, as used here, does! That's what the `-f` means. – ntc2 May 16 '14 at 00:02
  • Thank you for setting us straight, @ntc2! I should have looked/tested more carefully before opening my mouth. One more thing I learned about git. One less person on the internet is wrong, now! – ErikE May 16 '14 at 00:15
  • 4
    Has anyone been getting the error "error: Sparse checkout leaves no entry on working directory" when executing `git pull origin master` ? Before that error message, this was also print out: `*branch master -> FETCH_HEAD`. Can't seem to find the solution after looking at a lot of similar questions/posts. – LazerSharks Jul 18 '14 at 01:51
  • 2
    @Gnuey See @metator comment above! Use `git checkout master` instead. – Matthew Wilcoxson Aug 01 '14 at 12:55
  • 1
    @ntc2 : and you can do it fine without the -f, but then you have to pull it all when you run git pull origin master... regardless of the sparse checkout directory contents. – Erik Aronesty Aug 18 '14 at 16:39
  • 25
    Using this and `--depth=1` I cloned Chromium Devtools in 338 MB instead of 4.9 GB of full Blink source + history. Excellent. – Rudie Oct 22 '14 at 19:26
  • 3
    If like me you left some things out of `.git/info/sparse-checkout` the first time around, add them to that file and then do a `git reset --hard`. No additional `pull` required! – Jess Austin Nov 05 '14 at 19:22
  • 6
    This is not "subdirectory only", because when I do this I end up with a bunch of directories for the full path leading up to the subdirectory that I wanted. There seems to be no way to fit this into my existing source tree that I wanted to use it with, and still have it in a state where I can do commits. Man, every time I touch git I hate it more! – iforce2d Mar 03 '15 at 10:52
  • 2
    @iforce2d note that these sparse checkouts are just a hack and are not in any way how git is supposed to be used. If you need them, your project structure does not fit git. Git is not subversion. – Chronial Mar 03 '15 at 17:53
  • @Chronial "Git is not subversion" no, but in my case I'm trying to pull just the source portion of my repo down onto the server where it's hosted. I don't want all the other stuff! – Josh M. Jun 03 '15 at 20:59
  • @JoshM. Well, and iforce2d obviously was in different situation, as he wanted to commit. – Chronial Jun 04 '15 at 00:24
  • 1
    @Chronial,@Gnuey,@metator, Getting error "Sparse checkout leaves no entry on working directory". git checkout master also giving the same error. – CAD Dec 17 '15 at 07:31
  • It's not useful, by this approach it still need to clone all the repository instead of just part . – 柯鴻儀 Dec 18 '15 at 17:45
  • 1
    Worth pointing out that for the OP's case, the two sparse checkouts can share the same `.git` if they're on the same machine (or maybe even over NFS?), so you avoid having two copies of the full repo. (Or you could just have symlinks from two places into a normal full-tree clone of the repo, if it's ok to have them both parts of the working tree on the same filesystem.) – Peter Cordes Jun 15 '17 at 04:18
  • 3
    on windows `don't` use the quote sign by dir name: `dir`not `"dir"` – Mark Oct 27 '17 at 23:10
  • For those coming across the error "Sparse checkout leaves no entry on working directory" (@LazerSharks, @CAD,...), this may depend on the path of the folders. Please check it out whether https://stackoverflow.com/q/28852637/5459638 helps – XavierStuvw Aug 05 '19 at 06:47
  • I've created a ``bash`` function which avoids downloading the history, which retrieves a single branch and which retrieves a list of files or directories you need. See it here: https://stackoverflow.com/questions/60190759/how-do-i-clone-fetch-or-sparse-checkout-a-single-directory-or-a-list-of-directo – Richard Gomes Feb 12 '20 at 14:42
  • @Chromial: It might be worth adding partial clone to your answer, that would change it from "best answer, but doesn't fully address the question" to "the authoritative answer". I would love to see this selected as the actual answer. :) – Tao Apr 20 '20 at 15:49
  • https://stackoverflow.com/questions/4114887/is-it-possible-to-do-a-sparse-checkout-without-checking-out-the-whole-repository – R. Hoek Aug 28 '20 at 07:13
  • 2
    Fixed link to tutorial: https://jasonkarns.wordpress.com/2011/11/15/subdirectory-checkouts-with-git-sparse-checkout/ – Wheezil Jan 28 '21 at 13:58
  • 1
    this doesn't work... – Atmaram Apr 12 '22 at 02:27
  • This worked perfectly!!! I needed one directory one out of what appears to be about a hundred or so here https://github.com/eugenp/tutorials Needles to say I did not want a hundred repos. Using the commands outlined, I was able to check out the individual one needed. Tank you for posting this clear example – Greg May 28 '22 at 06:12
  • use $git sparse-checkout set – Timy Shark Jun 01 '22 at 18:29
  • this works properly. but I think "some/dir/" or "another/sub/tree" makes me confusing. – horoyoi o Aug 29 '23 at 03:16
1178

git clone --filter + git sparse-checkout downloads only the required files

E.g., to clone only files in subdirectory small/ in this test repository: https://github.com/cirosantilli/test-git-partial-clone-big-small-no-bigtree

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

You could also select multiple directories for download with:

git sparse-checkout set --no-cone small small2

This method doesn't work for individual files however, but here is another method that does: How to sparsely checkout only one single file from a git repository?

In this test, clone is basically instantaneous, and we can confirm that the cloned repository is very small as desired:

du --apparent-size -hs * .* | sort -hs

giving:

2.0K    small
226K    .git

That test repository contains:

  • a big/ subdirectory with 10x 10MB files
  • 10x 10MB files 0, 1, ... 9 on toplevel (this is because certain previous attempts would download toplevel files)
  • a small/ and small2/ subdirectories with 1000 files of size one byte each

All contents are pseudo-random and therefore incompressible, so we can easily notice if any of the big files were downloaded, e.g. with ncdu.

So if you download anything you didn't want, you would get 100 MB extra, and it would be very noticeable.

On the above, git clone downloads a single object, presumably the commit:

Cloning into 'test-git-partial-clone-big-small'...
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0
Receiving objects: 100% (1/1), done.

and then the final checkout downloads the files we requested:

remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), 10.19 KiB | 2.04 MiB/s, done.
remote: Enumerating objects: 253, done.
remote: Counting objects: 100% (253/253), done.
Receiving objects: 100% (253/253), 2.50 KiB | 2.50 MiB/s, done.
remote: Total 253 (delta 0), reused 253 (delta 0), pack-reused 0
Your branch is up to date with 'origin/master'.

Tested on git 2.37.2, Ubuntu 22.10, on January 2023.

TODO also prevent download of unneeded tree objects

The above method downloads all Git tree objects (i.e. directory listings, but not actual file contents). We can confirm that by running:

git ls-files

and seeing that it contains the directories large files such as:

big/0

In most projects this won't be an issue, as these should be small compared to the actual file contents, but the perfectionist in me would like to avoid them.

I've also created a very extreme repository with some very large tree objects (100 MB) under the directory big_tree: https://github.com/cirosantilli/test-git-partial-clone-big-small

Let me know if anyone finds a way to clone just the small/ directory from it!

About the commands

The --filter option was added together with an update to the remote protocol, and it truly prevents objects from being downloaded from the server.

The sparse-checkout part is also needed unfortunately. You can also only download certain files with the much more understandable:

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

but that method for some reason downloads files one by one very slowly, making it unusable unless you have very few files in the directory.

Another less verbose but failed attempt was:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git sparse-checkout set small

but that downloads all files in the toplevel directory: How to prevent git clone --filter=blob:none --sparse from downloading files on the root directory?

The dream: any directory can have web interface metadata

This feature could revolutionize Git.

Imagine having all the code base of your enterprise in a single monorepo without ugly third-party tools like repo.

Imagine storing huge blobs directly in the repo without any ugly third party extensions.

Imagine if GitHub would allow per file / directory metadata like stars and permissions, so you can store all your personal stuff under a single repo.

Imagine if submodules were treated exactly like regular directories: just request a tree SHA, and a DNS-like mechanism resolves your request, first looking on your local ~/.git, then first to closer servers (your enterprise's mirror / cache) and ending up on GitHub.

I have a dream.

The test cone monorepo philosophy

This is a possible philosophy for monorepo maintenance without submodules.

We want to avoid submodules because it is annoying to have to commit to two separate repositories every time you make a change that has a submodule and non-submodule component.

Every directory with a Makefile or analogous should build and test itself.

Such directories can depend on either:

  • every file and subdirectory under it directly at their latest versions
  • external directories can be relied upon only at specified versions

Until git starts supporting this natively (i.e. submodules that can track only subdirectories), we can support this with some metadata in a git tracked file:

monorepo.json

{
    "path": "some/useful/lib",
    "sha": 12341234123412341234,
}

where sha refers to the usual SHA of the entire repository. Then we need scripts that will checkout such directories e.g. under a gitignored monorepo folder:

monorepo/som/useful/lib

Whenever you change a file, you have to go up the tree and test all directories that have Makefile. This is because directories can depend on subdirectories at their latest versions, so you could always break something above you.

Related:

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 1
    Oddly, on macOS with git version 2.20.1 (Apple Git-117), it complains that "multiple filter-specs cannot be combined" – muru Dec 03 '19 at 09:06
  • 1
    @muru thanks for pointing this out. I had written the answer before the `tree:0` was out, and just supposed it would work to add another filter without testing. I saw now that there is however a new syntax to do it, updated (also untested, but copied from docs :-)) – Ciro Santilli OurBigBook.com Dec 03 '19 at 09:22
  • 3
    Sadly, no luck with the macOS git version. `fatal: invalid filter-spec 'combine:blob:none+tree:0'` Thanks anyway! Maybe it will work with newer versions. – muru Dec 06 '19 at 07:51
  • 1
    @muru was only added in git 2.24 as mentioned in answer BTW. Let me know if you test a newer git and it still fails. – Ciro Santilli OurBigBook.com Dec 06 '19 at 08:05
  • 2
    This fails when trying it on Windows 10 using GIT 2.24.1 (throws tons of "unable to read sha1 file of.." + "Unlink of file xxx failed."). Worked as a charm with same version on Linux. – Oyvind Dec 16 '19 at 11:40
  • 1
    @Oyvind thanks for the report. Let me know if you find anything about it, or consider opening a bug report upstream and linking to it on a comment. – Ciro Santilli OurBigBook.com Dec 16 '19 at 11:41
  • 3
    @Ciro Santilli This still fails with "unable to read sha1 file of..." in git version 2.26.1.windows.1. I opened a bug report: https://github.com/git-for-windows/git/issues/2590 – nharrer Apr 18 '20 at 16:43
  • 1
    @Ciro Santilli So far the clone/checkout worked well (on linux). However a `git status` or `git commit` would still pull in the full server repository unless I did a sparse checkout on top of it. Like this: https://gist.github.com/nharrer/33b1535b4630387ff2be3116513baa0b – nharrer Apr 18 '20 at 20:41
  • 1
    tried `git checkout master -- some/path` on a real repo and it downloads each file one by one making it rather slow. Any idea how to make it checkout all the files at once? – Faboor Jan 06 '21 at 11:03
  • 1
    @Faboor do you mean `some/path` is a directory and it downloads individual paths within the directory? Or you are making several calls with different paths `some/path1`, `some/path2`? For several calls, does `git checkout master -- some/path1 some/path2` not work? – Ciro Santilli OurBigBook.com Jan 06 '21 at 11:13
  • 2
    @CiroSantilli郝海东冠状病六四事件法轮功 `some/path` is a directory and `git checkout master -- some/path` correctly clones only files in that directory and its subdirs - but it does it one by one with a message like: `remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 51 bytes | 51.00 KiB/s, done.` These 4 lines repeated for each of the 90 files in the dir and its subdirs (this is on `git version 2.24.3 (Apple Git-128)`) – Faboor Jan 06 '21 at 11:33
  • 1
    @Faboor OK, then I don't know. I recommend opening a separate question, with a minimal reproducible test repo and the clone time difference between full/partial clone, and link to it from a comment here, I'd be curious to understand what is going on. – Ciro Santilli OurBigBook.com Jan 06 '21 at 11:38
  • 1
    It's much faster for me without `--filter=blob:none ` – Paul Beusterien Jan 13 '21 at 22:01
  • 1
    @PaulBeusterien can you give a sample repo and command? – Ciro Santilli OurBigBook.com Jan 13 '21 at 22:45
  • 1
    @PaulBeusterien reproduction at: https://github.com/isaacs/github/issues/1888 – Ciro Santilli OurBigBook.com Jan 14 '21 at 20:05
  • 1
    You almost certainly don't want the 'git sparse-checkout init --cone'. The effect of that is to also check-out all files in parent directories, as well as the tree you actually want. Without it you just get the tree you requested. – Mike Moreton Apr 12 '21 at 13:17
  • 1
    @MikeMoreton I didn't know that, let me know if you find a solution without it. Even with that, it is generally what most people will want I believe. – Ciro Santilli OurBigBook.com Apr 12 '21 at 19:03
  • 3
    @CiroSantilli新疆棉花TRUMPBANBAD - you've already found the solution! Just remove the --cone line and it will work fine. In your test repository try creating an additional file at the top level. If you follow your instructions, then you'll also get a copy of that file as well as the directory you want. Remove the 'git sparse-checkout init --cone' but follow all your other instructions, and you'll just get the directory tree you want. I'm not quite sure in what circumstances you'd want to use --cone! – Mike Moreton Apr 13 '21 at 11:57
  • 1
    @MikeMoreton thanks, I thought I had tested and found that it was necessary, but I can't reproduce now, so I think I was actually just copying commands from other people. Removed that `git sparse-checkout init --cone`. – Ciro Santilli OurBigBook.com Apr 13 '21 at 12:46
  • 2
    At least for git 2.33 and github.com `filter=tree:0` also prevents blobs from being downloaded (except for files in the toplevel directory of HEAD). So you don't need to combine it with `blob:none`. – Socowi Sep 11 '21 at 09:22
  • Works on GitLab (hosted), too (git client 2.32). – Danek Duvall Dec 18 '21 at 00:58
  • I think this does not exactly answer the question: it will indeed clone only the directory `d1`, **but in directory test-git-partial-clone** and not outside of any directory, like the command `svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static` would do for `static`. Here is another instance of this question: https://stackoverflow.com/questions/35097530/git-sparse-checkout-without-leading-directories – Gabriel Devillers Oct 07 '22 at 15:45
  • What would be the command to clone a specific tag instead of the HEAD of master? – Marinos An Oct 19 '22 at 10:59
  • @MarinosAn has you tried ideas from: https://stackoverflow.com/questions/31278902/how-to-shallow-clone-a-specific-commit-with-depth-1 ? – Ciro Santilli OurBigBook.com Oct 19 '22 at 11:07
  • What seems to have worked is (after clone and sparse checkout): `git fetch --depth 1 origin tagName` then `git switch --detach taggedCommitHash` – Marinos An Oct 19 '22 at 12:12
  • I'm still downloading files in the root of the repo when I only want a subdirectory – goweon Nov 11 '22 at 16:37
  • @goweon can you share your git version and exact command used? – Ciro Santilli OurBigBook.com Nov 11 '22 at 17:03
  • @CiroSantilliOurBigBook.com Thanks, but I figured it out. Turns out I needed to add `--no-checkout` during `clone`, and ` --no-cone` during `sparse-checkout set`. For some reason I can't figure out, it was defaulting to `--cone`. But for reference, I'm using git 2.38.1 on MINGW – goweon Nov 11 '22 at 17:15
  • I've just tried the first way (sparse-clone and then sparse-checkout) on git 2.39, and it still cloned the files found in the root of the repo (but didn't clone any directories, which is good). Any ideas of how to avoid fetching those files in the root? – Vadim Kantorov Dec 17 '22 at 17:49
  • @VadimKantorov I don't reproduce on git version 2.34.1 Ubuntu 22.04, here `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` clones only `d1/` and not `generate.sh` from https://github.com/cirosantilli/test-git-partial-clone-big-small seems to work. So you get `generate.sh` on that newer git? – Ciro Santilli OurBigBook.com Dec 17 '22 at 18:26
  • Yes, at least on my repo. I didn't try to clone your repo, I went directly to working with my repos – Vadim Kantorov Dec 17 '22 at 19:28
  • @VadimKantorov OK. Let me know if you find something I can try to reproduce. – Ciro Santilli OurBigBook.com Dec 17 '22 at 21:12
768

EDIT: As of Git 2.19, this is finally possible, as can be seen in this answer.

Consider upvoting that answer.

Note: in Git 2.19, only client-side support is implemented, server-side support is still missing, so it only works when cloning local repositories. Also note that large Git hosters, e.g. GitHub, don't actually use the Git server, they use their own implementation, so even if support shows up in the Git server, it does not automatically mean that it works on Git hosters. (OTOH, since they don't use the Git server, they could implement it faster in their own implementations before it shows up in Git server.)


No, that's not possible in Git.

Implementing something like this in Git would be a substantial effort and it would mean that the integrity of the clientside repository could no longer be guaranteed. If you are interested, search for discussions on "sparse clone" and "sparse fetch" on the git mailinglist.

In general, the consensus in the Git community is that if you have several directories that are always checked out independently, then these are really two different projects and should live in two different repositories. You can glue them back together using Git Submodules.

Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 6
    Depending on the scenario, you may want to use git subtree instead of git submodule. See http://alumnit.ca/~apenwarr/log/?m=200904#30 – C Pirate Aug 03 '09 at 17:12
  • 10
    @StijndeWitt: Sparse checkouts happen during `git-read-tree`, which is long after `get-fetch`. The question was not about checking out only a subdirectory, it was about *cloning* only a subdirectory. I don't see how sparse checkouts could possibly do that, since `git-read-tree` runs after the clone has already completed. – Jörg W Mittag Mar 06 '14 at 14:53
  • To help you show only the directory you want, you have to execute git read-tree -m -u HEAD – blio May 28 '15 at 06:08
  • 15
    Rather than this "stub", would you like for me to delete this answer so Chronial's can float to the top? You can't delete it yourself, because it's accepted, but a moderator can. You would keep the reputation you've earned from it, since it's so old. (I came across this because someone flagged it as "link-only". :-) – Cody Gray - on strike Aug 21 '17 at 17:49
  • You can download a subfolder by using this site here: http://kinolien.github.io/gitzip/ Just download a subfolder without entering a Github Access token ... – Legends Mar 01 '18 at 23:36
  • 1
    @CodyGray: Chronial answer *still* clones the entire repository, and *not* only a subdirectory. (The last paragraph even explicitly says so.) Cloning only a subdirectory is *not possible* in Git. The network protocol doesn't support it, the storage format doesn't support it. *Every single answer to this question* always clones the whole repository. The question is a simple Yes/No question, and the answer is two characters: No. If at all, my answer is unnecessarily *long*, not short. – Jörg W Mittag Aug 31 '18 at 14:45
  • 2
    @JörgWMittag: [Ciro Santili's answer](https://stackoverflow.com/a/52269934/1269037) seems to contradict you. – Dan Dascalescu Nov 04 '18 at 00:53
  • "if you have several directories that are always checked out independently, then these are really two different projects and should live in two different repositories" - what about checking out a microservice that's part of a monorepo, on the machine on which it is to be deployed? – Dan Dascalescu Nov 04 '18 at 00:55
  • @DanDascalescu note that my answer documents a very recent feature, possibly it was not merged when Jorg commented :-) I'm so excited... now you Googlers and all other big enterprises will be able to actually keep your monolithic Git repo sanely without extra tooling. Just hope web UI's start adding per folder metadata, e.g. per folder stars / permissions. – Ciro Santilli OurBigBook.com Nov 21 '18 at 20:50
  • Can you do it on TortoiseGit? – Alaa M. Jan 22 '20 at 11:54
  • I've created a ``bash`` function which avoids downloading the history, which retrieves a single branch and which retrieves a list of files or directories you need. See it here: https://stackoverflow.com/questions/60190759/how-do-i-clone-fetch-or-sparse-checkout-a-single-directory-or-a-list-of-directo – Richard Gomes Feb 12 '20 at 14:42
  • GitHub now has full support BTW as per updates on my answer :) – Ciro Santilli OurBigBook.com Feb 02 '21 at 19:31
447

You can combine the sparse checkout and the shallow clone features. The shallow clone cuts off the history and the sparse checkout only pulls the files matching your patterns.

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "finisht/*" >> .git/info/sparse-checkout
git pull --depth=1 origin master

You'll need minimum git 1.9 for this to work. Tested it myself only with 2.2.0 and 2.2.2.

This way you'll be still able to push, which is not possible with git archive.

udondan
  • 57,263
  • 20
  • 190
  • 175
  • what would be the echo path for this: `https://github.com/tastejs/todomvc/tree/master/examples/angularjs` just `examples/angularjs/*` ? – SuperUberDuper Jul 14 '15 at 13:56
  • I guess that would be `tastejs/todomvc/tree/master/examples/angularjs just examples/angularjs/*`. – udondan Jul 14 '15 at 14:49
  • 27
    This is useful, and may be the best available answer, but it still *clones* the content that you don't care about (if it is on the branch that you pull), even though it doesn't show up in the checkout. – Brent Bradburn Aug 25 '15 at 21:54
  • 1
    This does not work for me. --depth=1 is ignored, still tries to pull several thousand commits and about 100x too many files. – Tyguy7 Sep 18 '15 at 21:46
  • 1
    What is your git version? According to git help is the depth option available? – udondan Sep 19 '15 at 05:14
  • worked like a charm with git 1.9.1. exactly what I was looking for. – infoclogged Dec 10 '15 at 19:03
  • 2
    doesn't work for me when the last command is not `git pull --depth=1 origin master` but `git pull --depth=1 origin `. this is so strange, see my question here: http://stackoverflow.com/questions/35820630/how-do-i-checkout-a-sub-direcotry-in-a-huge-git-repo-with-specified-branch-and-w – Shuman Mar 06 '16 at 00:15
  • 5
    On Windows, the second-to-last line needs to omit the quotes, or the pull fails. – nateirvin Mar 31 '16 at 17:01
  • 1
    Still fetches all but checks out as expected. git `2.6.4`. – MadeOfAir May 14 '16 at 20:29
  • 4
    This still downloads all data! Found this solution, using svn: http://stackoverflow.com/a/18324458/2302437 – electronix384128 May 18 '16 at 00:09
  • 1
    How to only fetch files inside sub directory? – Babish Shrestha Sep 03 '16 at 04:49
  • @BabishShrestha set the path is the sparse-checkout file accordingly – udondan Sep 03 '16 at 10:30
  • @udondan I tried but It pulls the subdirectory folder every time. For example, the subdirectory is "src" and there are files inside src. Then with sparse checkout instead of fetching files inside src folder, it fetches files along with the src folder. – Babish Shrestha Sep 04 '16 at 15:08
  • This is the cleanest answer! @udondan, then, what should I do, so as to push the mirrored repo to under my own github account? I tried to `git remote add origin` again, but got `fatal: remote origin already exists.`. Moreover, when the original repo updated, how should I update my own github repo? Thx – xpt Apr 20 '17 at 22:18
  • This is suppose to be the correct answer, you are the best brah, simple and short – 0.sh Aug 26 '17 at 02:21
  • @udondan You missed a `git fetch --all`. This won't work if i have to pull a subdirectory and switch to an existing branch. – Aseem Upadhyay Mar 03 '18 at 06:33
  • This gets the job done. The only downside is that we lose our commit history and that's a big one for me – ksadjad Aug 07 '19 at 05:34
201

For other users who just want to download a file/folder from github, simply use:

svn export <repo>/trunk/<folder>

e.g.

svn export https://github.com/lodash/lodash.com/trunk/docs

(yes, that's svn here. apparently in 2016 you still need svn to simply download some github files)

Courtesy: Download a single folder or directory from a GitHub repo

Important - Make sure you update the github URL and replace /tree/master/ with '/trunk/'.

As bash script:

git-download(){
    folder=${@/tree\/master/trunk}
    folder=${folder/blob\/master/trunk}
    svn export $folder
}

Note This method downloads a folder, does not clone/checkout it. You can't push changes back to the repository. On the other hand - this results in smaller download compared to sparse checkout or shallow checkout.

Amit G
  • 2,293
  • 3
  • 24
  • 44
Anona112
  • 3,724
  • 4
  • 21
  • 30
  • 14
    only version which worked for me with github. The git commands checked out >10k files, the svn export only the 700 i wanted. Thanks! – Christopher Lörken Feb 01 '17 at 18:19
  • 4
    Tried doing this with `https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/trunk/udacity` but got `svn: E170000: URL 'https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/trunk/udacity' doesn't exist` error :( – zthomas.nc Feb 19 '17 at 21:55
  • 12
    @zthomas.nc You need to remove the 'trunk' preceding udacity, and replace /tree/master/ with /trunk/ instead. – Speedy Mar 22 '17 at 17:01
  • This doesn't create an actual repo I can commit to etc does it? – Dominic Mar 22 '17 at 22:55
  • 4
    This command was the one that worked for me! I just wanted to get a copy of a file from a repo so I could modify it locally. Good old SVN to the rescue! – Michael J Jun 02 '17 at 00:32
  • 3
    it works, but seems slow. takes a bit to start and then the files roll by relatively slowly – Aryeh Beitz Dec 24 '17 at 10:15
  • 1
    Does anyone have something similar which works on bitbucket? – N M Mar 22 '18 at 03:07
  • 1
    unfortunately does not work for me: seems to be hanging and nothing happens further :-( Is there a way to diagnose what's going on _under the hood_? I had to kill it after waiting a minute or so.... – Orco Jun 11 '18 at 23:35
  • 1
    Pro-tip: if you want to use the SVN method but you want to use git locally, use git svn clone instead – LaPingvino Apr 30 '19 at 14:44
  • It works nicely, but extremely slow. It takes 5 seconds to download 4 small files. – Slava Fomin II Nov 01 '19 at 11:58
  • 2
    I confirm that in 2020 it still works. It was just what I needed, so I just clone my project directory in the VM and the rest remains private in my GIT. Thank you very much for the solution. – junihh Aug 25 '20 at 20:41
  • 1
    What if you want to specify a branch that's not the master branch? – Godstime Osarobo Apr 01 '21 at 06:07
  • https://github.blog/2023-01-20-sunsetting-subversion-support/ – Jason S Mar 22 '23 at 19:37
92

2022 Answer

I'm not sure why there are so many complicated answers to this question. It can be done easily by doing a sparse cloning of the repo, to the folder that you want.

  1. Navigate to the folder where you'd like to clone the subdirectory.
  2. Open cmd and run the following commands.
  3. git clone --filter=blob:none --sparse %your-git-repo-url%
  4. cd %the repository directory%
  5. git sparse-checkout add %subdirectory-to-be-cloned%
  6. cd %your-subdirectory%

Voila! Now you have cloned only the subdirectory that you want!

Explanation - What are these commands doing really?

git clone --filter=blob:none --sparse %your-git-repo-url%

In the above command,

  • --filter=blob:none => You tell git that you only want to clone the metadata files. This way git collects the basic branch details and other meta from remote, which will ensure that your future checkouts from origin are smooth.
  • --sparse => Tell git that this is a sparse clone. Git will checkout only the root directory in this case.

Now git is informed with the metadata and ready to checkout any subdirectories/files that you want to work with.

git sparse-checkout add gui-workspace ==> Checkout folder

git sparse-checkout add gui-workspace/assets/logo.png ==> Checkout a file

Sparse clone is particularly useful when there is a large repo with several subdirectories and you're not always working on them all. Saves a lot of time and bandwidth when you do a sparse clone on a large repo.

Additionally, now in this partially cloned repo you can continue to checkout and work like you normally would. All these commands work perfectly.

git switch -c  %new-branch-name% origin/%parent-branch-name% (or) git checkout -b %new-branch-name% origin/%parent-branch-name% 
git commit -m "Initial changes in sparse clone branch"
git push origin %new-branch-name%
Tấn Nguyên
  • 1,607
  • 4
  • 15
  • 25
Evan MJ
  • 1,967
  • 13
  • 16
  • 4
    Good update. Could you replace the [old, confusing and obsolete](https://stackoverflow.com/a/57066202/6309) command [`git checkout`](https://git-scm.com/docs/git-checkout) with the more modern [`git switch`](https://git-scm.com/docs/git-switch)? `git switch -c %new-branch-name% origin/%parent-branch-name%` – VonC Aug 05 '22 at 19:33
  • Good catch, added that as well. @VonC – Evan MJ Aug 05 '22 at 19:44
  • 6
    If I clone from $CLONEDIR, the cloning creates another directory with the .git dir in it. lets call it REPO_DIRNAME. I have to `cd $CLONEDIR/$REPO_DIRNAME` before I can execute the `git sparse-checkout ...` bit. Could you amend? Ta :-) – Paddy3118 Aug 12 '22 at 11:02
  • I updated the "this is an older answer" headline you chose to add, and reduced its formatting. I am not sure why you did it, but with a more recent answer from 2022 it seems that it needed an update. But maybe you want to remove it altogether. – Yunnosch Sep 29 '22 at 06:19
  • @Yunnosch I had never added "this is an older answer" headline, I'm not sure what you're referring to in your comment. – Evan MJ Sep 29 '22 at 20:26
  • It is the line I edited, you can easily see in the editing history. – Yunnosch Sep 29 '22 at 20:39
  • need git 2.17 ? – MUY Belgium Aug 02 '23 at 15:13
81

If you never plan to interact with the repository from which you cloned, you can do a full git clone and rewrite your repository using

git filter-branch --subdirectory-filter <subdirectory>

This way, at least the history will be preserved.

justarandomguy
  • 331
  • 4
  • 15
hillu
  • 9,423
  • 4
  • 26
  • 30
  • 13
    For people that doesn't know the command, it is `git filter-branch --subdirectory-filter ` – Jaime Hablutzel Oct 26 '14 at 14:32
  • 10
    This method has the advantage that the subdirectory you choose becomes the root of the new repository, which happens to be exactly what I want. – Andrew Schulman Dec 23 '14 at 21:27
  • That's defenitly the best and easiest approach to use. Here's a one-step command using subdirectory-filter `git clone https://github.com/your/repo_xx.git && cd repo_xx && git filter-branch --subdirectory-filter repo_xx_subdir` – Alex Nov 03 '19 at 04:55
  • 3
    If you repo is tens of GB big, this would not help too much. – Adrian Maire Nov 11 '21 at 10:12
69

This looks far simpler:

git archive --remote=<repo_url> <branch> <path> | tar xvf -
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ErichBSchulz
  • 15,047
  • 5
  • 57
  • 61
  • 19
    When I do this on github I get fatal: Operation not supported by protocol. Unexpected end of command stream – Michael Fox Sep 25 '14 at 17:37
  • 1
    The protocol error could be because of HTTPS or : in the repo url. It could also be because of missing ssh key. – Umair A. Dec 07 '14 at 17:34
  • 2
    If you're using github you can use `svn export` instead – Milo Wielondek Jul 05 '15 at 15:25
  • 3
    Won't work wiht Github --> Invalid command: 'git-upload-archive 'xxx/yyy.git'' You appear to be using ssh to clone a git:// URL. Make sure your core.gitProxy config option and the GIT_PROXY_COMMAND environment variable are NOT set. fatal: The remote end hung up unexpectedly – Nianliang Jul 14 '15 at 15:19
  • 1
    Does this actually *clone* a piece of a repo (with git metadata), or just *download/extract* the subdirectory tree? – LarsH Oct 15 '15 at 19:21
  • 4
    The reason why this doesn't work with GitHub: "We don't support using git-archive to pull an archive directly from GitHub. You can either clone the repo locally and run git-archive, or click on the Download ZIP button on the repo page." https://github.com/xuwupeng2000/capistrano-scm-gitcopy/issues/16 – Donn Lee Aug 29 '16 at 23:17
  • 1
    @LarsH no, this just extracts the archive that is downloaded through some Git APIs. – kb1000 May 20 '18 at 16:06
64

Git 1.7.0 has “sparse checkouts”. See “core.sparseCheckout” in the git config manpage, “Sparse checkout” in the git read-tree manpage, and “Skip-worktree bit” in the git update-index manpage.

The interface is not as convenient as SVN’s (e.g. there is no way to make a sparse checkout at the time of an initial clone), but the base functionality upon which simpler interfaces could be built is now available.

Community
  • 1
  • 1
Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
41

It's not possible to clone subdirectory only with Git, but below are few workarounds.

Filter branch

You may want to rewrite the repository to look as if trunk/public_html/ had been its project root, and discard all other history (using filter-branch), try on already checkout branch:

git filter-branch --subdirectory-filter trunk/public_html -- --all

Notes: The -- that separates filter-branch options from revision options, and the --all to rewrite all branches and tags. All information including original commit times or merge information will be preserved. This command honors .git/info/grafts file and refs in the refs/replace/ namespace, so if you have any grafts or replacement refs defined, running this command will make them permanent.

Warning! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem.


Sparse checkout

Here are simple steps with sparse checkout approach which will populate the working directory sparsely, so you can tell Git which folder(s) or file(s) in the working directory are worth checking out.

  1. Clone repository as usual (--no-checkout is optional):

    git clone --no-checkout git@foo/bar.git
    cd bar
    

    You may skip this step, if you've your repository already cloned.

    Hint: For large repos, consider shallow clone (--depth 1) to checkout only latest revision or/and --single-branch only.

  2. Enable sparseCheckout option:

    git config core.sparseCheckout true
    
  3. Specify folder(s) for sparse checkout (without space at the end):

    echo "trunk/public_html/*"> .git/info/sparse-checkout
    

    or edit .git/info/sparse-checkout.

  4. Checkout the branch (e.g. master):

    git checkout master
    

Now you should have selected folders in your current directory.

You may consider symbolic links if you've too many levels of directories or filtering branch instead.


kenorb
  • 155,785
  • 88
  • 678
  • 743
  • Would **Filter branch** still allow you to `pull`? – sam Dec 17 '16 at 19:21
  • 2
    @sam: no. `filter-branch` would rewrite the parent commits so they'd have different SHA1 IDs, and thus your filtered tree would have no commits in common with the remote tree. `git pull` wouldn't know where to try to merge from. – Peter Cordes Jun 15 '17 at 04:06
  • This approach is mostly satisfying answer to my case. – Abbas Dec 15 '19 at 11:16
11

I wrote a script for downloading a subdirectory from GitHub.

Usage:

python get_git_sub_dir.py path/to/sub/dir <RECURSIVE>
david_adler
  • 9,690
  • 6
  • 57
  • 97
11

This will clone a specific folder and remove all history not related to it.

git clone --single-branch -b {branch} git@github.com:{user}/{repo}.git
git filter-branch --subdirectory-filter {path/to/folder} HEAD
git remote remove origin
git remote add origin git@github.com:{user}/{new-repo}.git
git push -u origin master
BARJ
  • 1,543
  • 3
  • 20
  • 28
  • 2
    Here be dragons. You get greeted by _WARNING: git-filter-branch has a glut of gotchas generating mangled history rewrites.._. Then the [git-filter-branch docs](https://htmlpreview.github.io/?https://raw.githubusercontent.com/newren/git-filter-repo/docs/html/git-filter-branch.html#SAFETY) has a rather long warning list. – Oyvind Dec 16 '19 at 12:05
9

here is what I do

git init
git sparse-checkout init
git sparse-checkout set "YOUR_DIR_PATH"
git remote add origin https://github.com/AUTH/REPO.git
git pull --depth 1 origin <SHA1_or_BRANCH_NAME>

Simple note

  • sparse-checkout

  • git sparse-checkout init many articles will tell you to set git sparse-checkout init --cone If I add --cone will get some files that I don't want.

  • git sparse-checkout set "..." will set the .git\info\sparse-checkout file contents as ...

    Suppose you don't want to use this command. Instead, you can open the git\info\sparse-checkout and then edit.


Example

Suppose I want to get 2 folderfull repo size>10GB↑ (include git), as below total size < 2MB

  1. chrome/common/extensions/api
  2. chrome/common/extensions/permissions
git init
git sparse-checkout init
// git sparse-checkout set "chrome/common/extensions/api/"
start .git\info\sparse-checkout    open the "sparse-checkut" file

/* .git\info\sparse-checkout  for example you can input the contents as below 
chrome/common/extensions/api/
!chrome/common/extensions/api/commands/      ! unwanted : https://www.git-scm.com/docs/git-sparse-checkout#_full_pattern_set
!chrome/common/extensions/api/devtools/
chrome/common/extensions/permissions/
*/

git remote add origin https://github.com/chromium/chromium.git
start .git\config

/* .git\config
[core]
    repositoryformatversion = 1
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[extensions]
    worktreeConfig = true
[remote "origin"]
    url = https://github.com/chromium/chromium.git
    fetch = +refs/heads/*:refs/remotes/Github/*
    partialclonefilter = blob:none  //  Add this line, This is important. Otherwise, your ".git" folder is still large (about 1GB)
*/
git pull --depth 1 origin 2d4a97f1ed2dd875557849b4281c599a7ffaba03
// or
// git pull --depth 1 origin master

  • partialclonefilter = blob:none

    I know to add this line because I know from: git clone --filter=blob:none it will write this line. so I imitate it.

git version: git version 2.29.2.windows.3

Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
Carson
  • 6,105
  • 2
  • 37
  • 45
  • this gives me error "Sparse checkout leaves no entry on working directory" on linux `git pull --depth 1 origin dev` – Sudhir N Apr 12 '23 at 07:28
8

Using Linux? And only want easy to access and clean working tree ? without bothering rest of code on your machine. try symlinks!

git clone https://github.com:{user}/{repo}.git ~/my-project
ln -s ~/my-project/my-subfolder ~/Desktop/my-subfolder

Test

cd ~/Desktop/my-subfolder
git status
Nasir Iqbal
  • 909
  • 7
  • 24
8

It worked for me- (git version 2.35.1)

git init
git remote add origin <YourRepoUrl>
git config core.sparseCheckout true
git sparse-checkout set <YourSubfolderName>
git pull origin <YourBranchName>
Abdul97j
  • 115
  • 1
  • 5
7

Just to clarify some of the great answers here, the steps outlined in many of the answers assume that you already have a remote repository somewhere.

Given: an existing git repository, e.g. git@github.com:some-user/full-repo.git, with one or more directories that you wish to pull independently of the rest of the repo, e.g. directories named app1 and app2

Assuming you have a git repository as the above...

Then: you can run steps like the following to pull only specific directories from that larger repo:

mkdir app1
cd app1
git init
git remote add origin git@github.com:some-user/full-repo.git
git config core.sparsecheckout true
echo "app1/" >> .git/info/sparse-checkout
git pull origin master

I had mistakenly thought that the sparse-checkout options had to be set on the original repository, but this is not the case: you define which directories you want locally, prior to pulling from the remote. The remote repo doesn't know or care about your only wanting to track a part of the repo.

Hope this clarification helps someone else.

Everett
  • 8,746
  • 5
  • 35
  • 49
  • this is a bit late but what do i do if i need all contents inside app1 and not the app1 directory – Swapnil Shende Aug 03 '20 at 15:26
  • That seems more like a cosmetic question, although it does seem like you don't have full freedom to "escape" the structure of the original repo. Maybe you could use symlinks? – Everett Dec 21 '20 at 13:23
  • you still have to download the whole repo it seems ``` $ mkdir com.unity.render-pipelines.core $ cd com.unity.render-pipelines.core/ $ git init $ git remote add origin https://github.com/Oculus-VR/Unity-Graphics.git $ git config core.sparsecheckout true $ echo "com.unity.render-pipelines.core/" >> .git/info/sparse-checkout $ git pull origin 2021.2/oculus-appsw-particles ``` The folder size is around 7mb , however ... ``` $ ... $ Receiving objects: 6% (24305/375290), 27.30 MiB | 121.00 KiB/s ``` – Vlad Apr 15 '22 at 21:50
6

Here's a shell script I wrote for the use case of a single subdirectory sparse checkout

coSubDir.sh

localRepo=$1
remoteRepo=$2
subDir=$3


# Create local repository for subdirectory checkout, make it hidden to avoid having to drill down to the subfolder
mkdir ./.$localRepo
cd ./.$localRepo
git init
git remote add -f origin $remoteRepo
git config core.sparseCheckout true

# Add the subdirectory of interest to the sparse checkout.
echo $subDir >> .git/info/sparse-checkout

git pull origin master

# Create convenience symlink to the subdirectory of interest
cd ..
ln -s ./.$localRepo/$subDir $localRepo
fduff
  • 3,671
  • 2
  • 30
  • 39
jxramos
  • 7,356
  • 6
  • 57
  • 105
  • 2
    Nice script, only something which should be fixed is the symlink, should be `ln -s ./.$localRepo/$subDir $localRepo` instead of `ln -s ./.$localRepo$subDir $localRepo` – valentin_nasta Mar 16 '18 at 08:33
4
git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "<path you want to clone>/*" >> .git/info/sparse-checkout
git pull --depth=1 origin <branch you want to fetch>

Example for cloning only Jetsurvey Folder from this repo

git init MyFolder
cd MyFolder 
git remote add origin git@github.com:android/compose-samples.git
git config core.sparsecheckout true
echo "Jetsurvey/*" >> .git/info/sparse-checkout
git pull --depth=1 origin main
wadali
  • 2,221
  • 1
  • 20
  • 38
3

I wrote a .gitconfig [alias] for performing a "sparse checkout". Check it out (no pun intended):

On Windows run in cmd.exe

git config --global alias.sparse-checkout "!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p \"$L/.git/info\" && cd \"$L\" && git init --template= && git remote add origin \"$1\" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo \"$2\" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f"

Otherwise:

git config --global alias.sparse-checkout '!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p "$L/.git/info" && cd "$L" && git init --template= && git remote add origin "$1" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo "$2" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f'

Usage:

# Makes a directory ForStackExchange with Plug checked out
git sparse-checkout https://github.com/YenForYang/ForStackExchange Plug

# To do more than 1 directory, you have to specify the local directory:
git sparse-checkout https://github.com/YenForYang/ForStackExchange ForStackExchange Plug Folder

The git config commands are 'minified' for convenience and storage, but here is the alias expanded:

# Note the --template= is for disabling templates.
# Feel free to remove it if you don't have issues with them (like I did)
# `mkdir` makes the .git/info directory ahead of time, as I've found it missing sometimes for some reason
f(){
    [ "$#" -eq 2 ] && L="${1##*/}" L=${L%.git} || L=$2;
    mkdir -p "$L/.git/info"
        && cd "$L"
        && git init --template=
        && git remote add origin "$1"
        && git config core.sparseCheckout 1;
    [ "$#" -eq 2 ]
        && echo "$2" >> .git/info/sparse-checkout
        || {
            shift 2;
            for i; do
                echo $i >> .git/info/sparse-checkout;
            done
        };
    git pull --depth 1 origin master;
};
f
YenForYang
  • 2,998
  • 25
  • 22
3

Lots of great responses here, but I wanted to add that using the quotations around the directory names was failing for me on Windows Sever 2016. The files simply were not being downloaded.

Instead of

"mydir/myfolder"

I had to use

mydir/myfolder

Also, if you want to simply download all sub directories just use

git sparse-checkout set *
3

@Chronial 's anwser is no longer applicable to recent versions, but it was a useful anwser as it proposed a script.

Given information I gathered and the fact that I wanted to checkout only a subdirectory of a branch, I created the following shell function. It gets a shallow copy of only the most recent version in the branch for the provided directories.

function git_sparse_clone_branch() (
  rurl="$1" localdir="$2" branch="$3" && shift 3

  git clone "$rurl" --branch "$branch" --no-checkout "$localdir" --depth 1  # limit history
  cd "$localdir"

  # git sparse-checkout init --cone  # fetch only root file

  # Loops over remaining args
  for i; do
    git sparse-checkout set "$i"
  done

  git checkout "$branch"
)

So example use:

git_sparse_clone_branch git@github.com:user/repo.git localpath branch-to-clone path1_to_fetch path2_to_fetch

In my case the clone was "only" 23MB versus 385MB for the full clone.

Tested with git version 2.36.1 .

le_top
  • 445
  • 3
  • 12
2

If you're actually ony interested in the latest revision files of a directory, Github lets you download a repository as Zip file, which does not contain history. So downloading is very much faster.

weberjn
  • 1,840
  • 20
  • 24
1

While I hate actually having to use svn when dealing with git repos :/ I use this all the time;

function git-scp() (
  URL="$1" && shift 1
  svn export ${URL/blob\/master/trunk}
)

This allows you to copy out from the github url without modification. Usage;

--- /tmp » git-scp https://github.com/dgraph-io/dgraph/blob/master/contrib/config/kubernetes/helm                                                                                                                  1 ↵
A    helm
A    helm/Chart.yaml
A    helm/README.md
A    helm/values.yaml
Exported revision 6367.

--- /tmp » ls | grep helm
Permissions Size User    Date Modified    Name
drwxr-xr-x     - anthony 2020-01-07 15:53 helm/
expelledboy
  • 2,033
  • 18
  • 18
1

Lots of good ideas and scripts above. I could not help myself and combined them into a bash script with help and error checking:

#!/bin/bash

function help {
  printf "$1
Clones a specific directory from the master branch of a git repository.

Syntax:
  $(basename $0) [--delrepo] repoUrl sourceDirectory [targetDirectory]

If targetDirectory is not specified it will be set to sourceDirectory.
Downloads a sourceDirectory from a Git repository into targetdirectory.
If targetDirectory is not specified, a directory named after `basename sourceDirectory`
will be created under the current directory.

If --delrepo is specified then the .git subdirectory in the clone will be removed after cloning.


Example 1:
Clone the tree/master/django/conf/app_template directory from the master branch of
git@github.com:django/django.git into ./app_template:

\$ $(basename $0) git@github.com:django/django.git django/conf/app_template

\$ ls app_template/django/conf/app_template/
__init__.py-tpl  admin.py-tpl  apps.py-tpl  migrations  models.py-tpl  tests.py-tpl  views.py-tpl


Example 2:
Clone the django/conf/app_template directory from the master branch of
https://github.com/django/django/tree/master/django/conf/app_template into ~/test:

\$ $(basename $0) git@github.com:django/django.git django/conf/app_template ~/test

\$ ls test/django/conf/app_template/
__init__.py-tpl  admin.py-tpl  apps.py-tpl  migrations  models.py-tpl  tests.py-tpl  views.py-tpl

"
  exit 1
}

if [ -z "$1" ]; then help "Error: repoUrl was not specified.\n"; fi
if [ -z "$2" ]; then help "Error: sourceDirectory was not specified."; fi

if [ "$1" == --delrepo ]; then
  DEL_REPO=true
  shift
fi

REPO_URL="$1"
SOURCE_DIRECTORY="$2"
if [ "$3" ]; then
  TARGET_DIRECTORY="$3"
else
  TARGET_DIRECTORY="$(basename $2)"
fi

echo "Cloning into $TARGET_DIRECTORY"
mkdir -p "$TARGET_DIRECTORY"
cd "$TARGET_DIRECTORY"
git init
git remote add origin -f "$REPO_URL"
git config core.sparseCheckout true

echo "$SOURCE_DIRECTORY" > .git/info/sparse-checkout
git pull --depth=1 origin master

if [ "$DEL_REPO" ]; then rm -rf .git; fi
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85
1

You can still use svn:

svn export https://admin@domain.example/home/admin/repos/finisht/static static --force

to "git clone" a subdirectory and then to "git pull" this subdirectory.

(It is not intended to commit & push.)

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
1

degit makes copies of git repositories. When you run degit some-user/some-repo, it will find the latest commit on https://github.com/some-user/some-repo and download the associated tar file to ~/.degit/some-user/some-repo/commithash.tar.gz if it doesn't already exist locally. (This is much quicker than using git clone, because you're not downloading the entire git history.)

degit <https://github.com/user/repo/subdirectory> <output folder>

Find out more https://www.npmjs.com/package/degit

1

(extending this answer )

Cloning subdirectory in specific tag

If you want to clone a specific subdirectory of a specific tag you can follow the steps below.

I clone the distribution/src/main/release/samples/ subdirectory of cxf github repo in cxf-3.5.4 tag.

Note: if you attempt to just clone the above repo you will see that it is very big. The commands below clones only what is needed.

git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/
git fetch --depth 1 origin cxf-3.5.4
# This is the hash on which the tag points, however using the tag does not work.
git switch --detach 3ef4fde

Cloning subdirectory in specific branch

I clone the distribution/src/main/release/samples/ subdirectory of cxf github repo in 2.6.x-fixes branch.

git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf --branch 2.6.x-fixes
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/
Marinos An
  • 9,481
  • 6
  • 63
  • 96
0
  • If you want to clone

    git clone --no-checkout <REPOSITORY_URL>
    cd <REPOSITORY_NAME>
    
    1. Now set the specific file / directory you wish to pull into the working-directory:
      git sparse-checkout set <PATH_TO_A_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>
      
    2. Afterwards, you should reset hard your working-directory to the commit you wish to pull.

      For example, we will reset it to the default origin/master's HEAD commit.

      git reset --hard HEAD
      
  • If you want to git init and then remote add

    git init
    git remote add origin <REPOSITORY_URL>
    
    1. Now set the specific file / directory you wish to pull into the working-directory:
      git sparse-checkout set <PATH_TO_A_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>
      
    2. Pull the last commit:
      git pull origin master
      

NOTE:

If you want to add another directory/file to your working-directory, you may do it like so:

git sparse-checkout add <PATH_TO_ANOTHER_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>

If you want to add all the repository to the working-directory, do it like so:

git sparse-checkout add *

If you want to empty the working-directory, do it like so:

git sparse-checkout set empty

If you want, you can view the status of the tracked files you have specified, by running:

git status

If you want to exit the sparse mode and clone all the repository, you should run:

git sparse-checkout set *
git sparse-checkout set init
git sparse-checkout set disable
0

I don't know if anyone succeeded pulling specific directory, here is my experience: git clone --filter=blob:none --single-branch <repo>, cancel immediately while downloading objects, enter repo, then git checkout origin/master <dir>, ignore errors (sha1), enter dir, repeat checkout (using new dir) for every sub-directory. I managed to quickly get source files in this way

Ilir Liburn
  • 222
  • 1
  • 2
  • 6
0

For macOS users

For zsh users (macOS users, specifically) cloning Repos with ssh, I just create a zsh command based on the answer by @Ciro Santilli:

requirement: The version of git matters. It doesn't work on 2.25.1 because of the --sparse option. Try upgrade your git to the latest version. (e.g. tested 2.36.1)

example usage:

git clone git@github.com:google-research/google-research.git etcmodel

code:

function gitclone {
    readonly repo_root=${1?Usage: gitclone repo.git sub_dir}
    readonly repo_sub=${2?Usage: gitclone repo.git sub_dir}
    echo "-- Cloning $repo_root/$repo_sub"
    git clone \
      --depth 1 \
      --filter=tree:0 \
      --sparse \
      $repo_root \
    ;
    repo_folder=${repo_root#*/}
    repo_folder=${repo_folder%.*}
    cd $repo_folder
    git sparse-checkout set $repo_sub
    cd -
}


gitclone "$@"
NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
-1

So i tried everything in this tread and nothing worked for me ... Turns out that on version 2.24 of Git (the one that comes with cpanel at the time of this answer), you don't need to do this

echo "wpm/*" >> .git/info/sparse-checkout

all you need is the folder name

wpm/*

So in short you do this

git config core.sparsecheckout true

you then edit the .git/info/sparse-checkout and add the folder names (one per line) with /* at the end to get subfolders and files

wpm/*

Save and run the checkout command

git checkout master

The result was the expected folder from my repo and nothing else Upvote if this worked for you

Patrick Simard
  • 2,294
  • 3
  • 24
  • 38