9

I have a git repo on Github, looks like so:

.git/
assets/
  tools/
    a.sh
    b.sh
    c.sh

of course the .git folder is not in version control, but you get the idea. And I want to copy the tools/*.sh to a local directory, at this path:

./scripts/git/tools

My current directory is a git repo that shares a totally different history than the remote git repo, I just want to copy some files from the remote, I don't want to merge anything.

I figure I could do something like this:

git clone --path assets/tools/*.sh "$remote_repo"  ./scripts/git/tools

but that command is not real, is there some command I can use to do this?

the only other way I can think of doing it, is cloning the whole repo to some temp directory and then copying the files over, but that's not as much fun.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Possibly a duplicate of https://stackoverflow.com/questions/180052/checkout-subdirectories-in-git – lakshayg Jul 07 '18 at 04:23
  • Possible duplicate of [Checkout subdirectories in Git?](https://stackoverflow.com/questions/180052/checkout-subdirectories-in-git) – lakshayg Jul 07 '18 at 04:55
  • This may be useful, in learning about trimming the fat of big repos: https://www.atlassian.com/blog/git/handle-big-repositories-git – johnb003 Jul 08 '18 at 07:00

2 Answers2

16

For Github repos, you can clone any sub-directories of any repo using the tool github-clone

Install it like this:

pip install github-clone

Then use it like this:

ghclone https://github.com/HR/Crypter/tree/dev/app
Jiyan Akgül
  • 158
  • 10
CryptoGeek
  • 171
  • 1
  • 3
  • 1
    This is a very straight forward answer. – blockhead Jul 20 '21 at 13:38
  • just use ```pip install github-clone``` when you get an git protocol error from the installation command – Jiyan Akgül May 04 '22 at 15:23
  • Note: This answer only considers one specific application (github) and does not answer the more general question about git. git (the client and server tools) are not just for github. – Jay M May 16 '23 at 11:27
2

the only other way I can think of doing it, is cloning the whole repo to some temp directory and then copying the files over, but that's not as much fun.

Yet, this is preferable, considering that even a partial clone, if it existed, would still preserve the asset/tools path structure.
You can do a sparse checkout elsewhere, and then copy your files.

With Git 2.17/2.18, you can attempt a partial or narrow clone, to clone only a subfolder, but that supposes the remote server does support this new feature.

Another more classic option is to use (if the remote repository supports it) the archive (zip file) of the latest HEAD: see "How to checkout only one file from git repository ('sparse checkout')?", but apply to a subfolder of the archive, instead of a file.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    The only thing I would add is maybe do a shallow clone: git clone --depth 1 --branch [branch_name] --single-branch – johnb003 Jul 08 '18 at 06:54
  • @johnb003 I agree: that would really limit the amount of data cloned. – VonC Jul 08 '18 at 07:16