12

I understand how to configure a sparse checkout in Git, but I was wondering if it's possible to eliminate the leading directories. For example, say I have a Git repository with a folder structure like so:

  • folder1/foo
  • folder2/foo/bar/stuff
  • folder3/another/foo/bar/stuff2

And I only want the contents inside stuff2. For sparse checkout of those directories, the .git/info/spare-checkout file would look like this:

folder3/another/foo/bar/stuff2/

My problem is after I'm finished with the sparse checkout, I don't see stuff2 or the contents of stuff2 inside the root of my directory; I see folder3 and then all of the subdirectories leading up to stuff2.

I'm guessing the answer to this question is that it's not possible with Git, but I just wanted to make sure. This is one (and only) behavior of SVN that I miss; the ability to run svn co https://url.com/myrepo/folder3/another/foo/bar/stuff2 . and have the contents dumped into the current directory.

Scott Crooks
  • 1,523
  • 4
  • 24
  • 39
  • What's missing from `git checkout folder3/another/foo/bar/stuff2` ? I probably don't get the question... – Martin G Jan 30 '16 at 03:17
  • 6
    The question is not a duplicate and has not been answered as far as I can see. The question is not how to do sparse checkouts (op knows that) or how to avoid a full repository clone (--depth 0). The question is how to avoid getting the full deep directory structure with sparse checkouts. Answer http://stackoverflow.com/questions/180052/checkout-subdirectories-in-git/5394612#5394612 notes that it is not possible, but without explanation and is 5 years old. – djk May 03 '16 at 21:46
  • Related: https://stackoverflow.com/questions/2336580/alter-repository-directory-structure-using-sparse-checkout – Gabriel Devillers Oct 07 '22 at 15:35

1 Answers1

0

Some tools like git hub have the ability to view a given folder directly. However to operate on only one folder at a time is forbidden in git as the whole repository must be present to perform the hash of the new commit and make the diff for the commit

It is also discussed here How to git-pull all but one folder

Community
  • 1
  • 1
  • See my comment in @DominicEU's answer – Scott Crooks Jan 30 '16 at 03:23
  • From my understanding you have files that are in git that you want to place in the /path/to/config. I would checkout the contents of the git repository some where permanent. Then make symbolic links to the file in git placing the links in the /path/to/config. If this works for you I can place the commands in another answer. – Mike Forsberg Jan 30 '16 at 03:29
  • Mike: Technically it's the other way around; I have files in `/path/to/config` that are currently **not** in Git. I created the directory where I want to put the `/path/to/config` files (aka the `stuff2` dir) but got the problem I described. – Scott Crooks Jan 30 '16 at 03:32
  • Right so first copy the config files to the git repository where you want to store them. Then make a symbolic link in the /path/to/config. Then when ever the git repo changes, the file will be updated. – Mike Forsberg Jan 30 '16 at 03:33
  • This answer does not take sparse checkout into account and is wrong since version 1.7. It seems Scott knows already more than the answer provides. – djk May 03 '16 at 21:36
  • Operating on only one folder should not be a problem as long as the whole repository is still in `.git`. – Anders Lindén Jun 04 '19 at 14:56