2

I have a repo that contains about 20 folders, created when I converted from SVN to Git. Is it possible to checkout a single folder from a Git repository (on Bitbucket)? Or do I have to make each of those folders a separate repo?

sehe
  • 374,641
  • 47
  • 450
  • 633
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • The answer is: yes. Unless you like to jump through (many) hoops, adn still end up with a limited solution – sehe Dec 21 '13 at 22:21
  • 1
    *Yes* to which question? – SpokaneDude Dec 21 '13 at 22:23
  • 2
    Yes to "Or do I have to make each of those folders a separate repo". There's a reason why you should try to ask a single clear question :) Anyhow, I hope I amused you with an approach that could work in my answer below. – sehe Dec 21 '13 at 22:32
  • or of: http://stackoverflow.com/questions/600079/is-there-any-way-to-clone-a-git-repositorys-sub-directory-only – sleske Dec 21 '13 at 22:42

1 Answers1

6

Edit Sparse checkouts could be basically this but better (see linked questions). Only, my answer takes care to do a shallow clone as well as doing only the sparse checkout.

You could hack it in two (well, roughly) steps:

  1. checkout a shallow copy

    cd /tmp
    git clone --depth=1 --bare git@myhosted_repo/project.git
    
    (cd project.git && git branch master)
    
  2. now - setup your working tree (separately)

    mkdir /tmp/workdir
    cd /tmp/workdir
    git init .
    
    # add the shallow clone as a remote
    git remote add --fetch shallow /tmp/project.git
    
  3. the real hack:

    # read a subtree from the remote, shallow branch
    git read-tree --prefix somedir -u shallow/master:testcases/
    

    (assuming your repo has a directory called testcases)

You'd end up with a working dir containing just the files from testcases/ in a subdirectory called somedir

The upshot:

  • It gets only a single revision from the hosted repo. Good(TM)
  • It only ever checks out the single subfolder from that revision. Good(TM)
  • It's arcane. It will work especially bad if you intend to put somedir in some other repo (subtree merges can be done, but I wouldn't burn my fingers there)
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Failing on on git branch master step. Then fetch gives warning: reject refs/remotes/shallow/master because shallow roots are not allowed to be updated. Also, fatal: Not a valid object name shallow/master:testcases... – jrosell Mar 04 '16 at 08:54
  • @jrosell of course, shallow clones have limitations. If you look at the question, you see an example of a situation where shallow clones could be useful. Yours is not. You can unshallow your clone: `git fetch --unshallow origin` in your cloned repo – sehe Mar 04 '16 at 09:34