DEV Community

loading...
Cover image for Clone just the sub-folder in GIT 🔥

Clone just the sub-folder in GIT 🔥

insanenaman profile image Naman Gupta Updated on ・2 min read

Background

Hi there, Its Naman. I am sure that if you work with Git then you must have stumble across one of these situations at least once:

  • You need only one subfolder but don't want to clone the entire repo
  • You only need to play around the code and you don't care about the whole Git history.

For the first problem, there is no straightforward way/command available (Correct me if I am wrong). If the repo is small then you can clone the whole repo. Easy peasy, you won't think twice but if the repo is huge then you might think twice or maybe thrice. 😛

For the second problem, if you don't care about the code history then cloning history is overhead for you. We should skip if we can (A native way available but isn't friendly).

Solving problem 🛠 :

I found this awesome package DeGit, created by Rich Harris (Creator of RollupJS and Svelte) quite a while back. This package is way easy to install and use. I use it frequently.

Note: Before moving forward make sure you have Node.js installed in your system. I recommend installing Node.js via nvm(Node Version Manager)

1- Install npm package 📦
Installing DeGit npm package

Use sudo npm i -g degit if your terminal shows permission error.

2- Cloning Sub-Folder ⬇️
Syntax: degit <repo-link/subfolder-name> <output-folder>
Alt Text
Here we are cloning components sub-folder and saving in our system with the same name.

3- Cloning whole repo without whole Git history

Syntax: degit <repo-link> <output-folder>

Alt Text

I ❤️ this. It's super fast. 🚀

That's it for today. Feel free to provide any type of feedback. I highly appreciate that. Meanwhile, you can find me on Twitter @InsaneNaman.

Signing off. See you soon.

Discussion (2)

pic
Editor guide
Collapse
waleedtageldeen profile image
Waleed Tageldeen

To get only the latest n commits not the full history you can use the --depth option with git clone. Check this example

Collapse
insanenaman profile image
Naman Gupta Author

Thanks for pointing out. Yes it's a way and author of this package has also addressed here - github.com/Rich-Harris/degit#wait-...