Checkout sub directories in git (sparse checkouts)

SVN externals is a really nice feature I used a lot. Switching to git I was really missing it. Of course there is git-submodule but it’s not the same. Now, with version 1.7 git supports so called sparse checkouts which allow you to only include specific sub directories of a repository in your project. You still need to clone the whole repository, but afterwards you can tell git to only show the specified sub directories.

Here is how it works:

  1. clone the other repository (in my case, for a Rails app, I did it in vendor/plugins/)
    git clone <repository_url> <directory>
  2. cd to <directory>
    cd <directory>
  3. enable sparsecheckout
    git config core.sparsecheckout true
  4. add directories you want to have in your checkout to .git/info/sparse-checkout, e.g.
    echo app/models/ > .git/info/sparse-checkout
    echo lib/ >> .git/info/sparse-checkout
  5. run read-tree
    git read-tree -m -u HEAD

ls now shows

app lib

Forgot a directory you wanted to include? Just repeat step 4 and 5.

22 comments

  1. Awesome! Exactly what I was looking for…

  2. What if I want to checkout all but one? Is there an exclusion command?

    And, what about files? Do they have to be added to .git/info/sparse-checkout as well?

    Thanks

  3. These command should be run on server side on client also let me how can take single file git pull and git push command.

  4. Is it possible to configure multiple sparse ?

    For example I want a sparse checkout for each jenkins job I have. So I will like Job A to only get /FolderA when executed and Job B only to get /FolderB when executed. As the config looks global I’m confused :S

    thanks

  5. My friend smiled and told me to look into this blog post and i noticed it relatively impressive. I will bookmark it for a future needed along with i might tell a friends to examine this blog post. Thanks plenty for writing.

  6. Thanks for that explanation. I was pointed here from a stackoverflow answer that explains you can do this to submodules too.

    The only thing I can’t figure out is how to commit this to the parent repository so that everyone checking out the parent repository will get the sparse checkout for submodules.

    Any idea?

  7. I like the helpful information you provide in your articles.

    I’ll bookmark your weblog and check again here frequently.
    I’m quite certain I will learn lots of new stuff right here!

    Good luck for the next!

  8. Can I simply say what a relief to discover somebody that actually knows what they are talking about over
    the internet. You certainly know how to bring an issue to light and make it important.

    More people should read this and understand this side of your story.
    I can’t believe you are not more popular because you
    definitely have the gift.

  9. 按照你说的方法,实现了,想要的功能。可是,我做好了的“本地库”推送 “http://git.oschina.net/”,或者在本地clone一份刚刚做好的“本地库”的时候,新的“版本库”有还原到第二步了。有什么方法使得“新clone版本库”的和按你的方法整理过的“版本库”一样的啊?

  10. Useful info. Lucky me I found your web site unintentionally,
    and I’m surprised why this twist of fate didn’t happened in
    advance! I bookmarked it.

  11. Everything is very open with a very clear explanation of the challenges.
    It was really informative. Your website is useful. Thank you for sharing!

  12. Every weekend i used to pay a visit this website,
    for the reason that i want enjoyment, since this this
    web page conations really fastidious funny material too.

  13. The passenger provides points on your licence
    another pair of eyes on the road, and the matter was adjourned until
    next week. But the storm was still strong enough to kill three
    people, including a lifting of the ban as she claimed she would be if she had her own
    car to drive his car. A man who was on the other hand, 68 percent of those surveyed last year, achieving seventh
    place.

  14. Just want to say your article is as amazing.
    The clearness in your post is simply excellent and i
    can assume you are an expert on this subject.
    Fine with your permission let me to grab your feed to
    keep updated with forthcoming post. Thanks a million and
    please keep up the rewarding work.

  15. Hi, I read your new stuff regularly. Your humoristic style is witty, keep
    doing what you’re doing!

  16. Does your website have a contact page? I’m having a tough time locating it but, I’d like to shoot you an email. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it expand over time.

  17. Valuable information. Lucky me I found your site by accident, and I am shocked why this accident did not happened earlier! I bookmarked it. dbdddecbdgge

  18. Hola! I’ve been reading your weblog for a long time now
    and finally got the courage to go ahead and give you a shout out from Atascocita Texas!
    Just wanted to tell you keep up the great
    job!

  19. Just desire to say your article is as astounding.
    The clarity for your put up is simply nice and i can assume you’re an expert in this subject.

    Fine together with your permission let me to seize your RSS feed to
    stay up to date with imminent post. Thanks a million and please carry on the enjoyable work.

  20. hi . i want to clone nly 1 perticular file from thre repository, can u tell me the cmmand for that…

  21. I’m not sure exactly why but this site is loading incredibly
    slow for me. Is anyone else having this issue or is it a
    problem on my end? I’ll check back later and see if the problem
    still exists.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>