5

How to clone a subdirectory of a git project (not downloading the zip).

 3 years ago
source link: https://dev.to/sanixdarker/git-cloning-a-subdirectory-of-a-gi-project-2mld
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Cover image for How to clone a subdirectory of a git project (not downloading the zip).

How to clone a subdirectory of a git project (not downloading the zip).

Aug 21

・1 min read

Have you ever wonder a way to clone a specific folder in a git repository ?

hop hop hop, easy.. he was not talking about Downloading a ZIP(like DownGit does), but only cloning a sub-dir, just to make things clear !

I got that question from a friend, and i wrote a small and interesting bash script to do so :

#!/bin/bash

# First parameter is the sub-directory-absolute-path
# Second parameter is the link of the repo

# A smart split to get the repo-name, with / as a separator
REPO_NAME="$(echo $2 | grep -oE '[^/]+$')"

git init $REPO_NAME
cd $REPO_NAME

git remote add origin $2
git config core.sparsecheckout true

# Specipy the sub directory
echo "$1/*" >> .git/info/sparse-checkout
# then get it, the depth is the way too far wher you can go...
git pull origin master

# and badaboum, you only get your sub-dir
# this script is functionnal for github/gitlab and bitbucket
Enter fullscreen modeExit fullscreen mode

Source Code : Gist-Code

Have FUN !


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK