How To Clone Specific Subdirectory/Branch with Git

JVDY
3 min readJul 23, 2023

--

Git doesn’t directly support cloning subdirectories. Use “ — depth”, “sparse-checkout”, and “checkout” commands to do a partial pull.

“--depth n” refers to the last n commits you want to pull.
(fyi: “--depth 2” means you want the last 2 commits, not the second to last commit.)
sparse-checkout” just means you want a partial pull.
checkout” means you’re done. (This will replace the “pull” command)

Steps:

  1. Clone repo from latest commit only using “ — depth 1” with your “clone” command.
  2. Move into the new folder using the “cd” command or where ever you want to put it.
  3. Set the desired subdirectory you want to clone using “sparse-checkout”
  4. “checkout”.

Example: Cloning a Specific Subdirectory from the Last Commit.

~$ git clone --depth 1 --no-checkout INSERT_REPO_REMOTE_URL
//root folder of the same name is created
~$ cd INSERT_ROOT_FOLDER_NAME/
~$ git sparse-checkout set INSERT_DESIRED_SUBDIRECTORY_RELATIVE-PATH
/* the relative path is the path relative to the current -
- working directory, which should be the cloned root folder above */
~$ git checkout

… and that’s it.

Other Options You Can Add.

Only Pull a Subdirectory of a Branch (Other Than Your Main Branch) from the Last Commit.

You can be more specific and choose a subdirectory of a certain branch by adding this option to the “clone” command in line 1:

--branch INSERT_BRANCH_NAME

example:

    //add --branch option here
~$ git clone --depth 1 --branch INSERT_BRANCH_NAME --no-checkout INSERT_REPO_REMOTE_URL
~$ cd INSERT_ROOT_FOLDER/
~$ git sparse-checkout set INSERT_SUBDIRECTORY_RELATIVE-PATH
//add --branch option here
~$ git checkout --branch INSERT_BRANCH_NAME

Specify an Entire Branch from the Last Commit Only.

If you want to just clone a branch from the last commit then just
add “ — branch” followed by it’s name, then use “ — single branch” followed by your repository’s full URL from Github.

Replace line 1 with this line.

~$ git clone -depth 1 --branch INSERT_BRANCH_NAME --single-branch INSERT_REPO_REMOTE_URL

Example: Clone a specific branch with full commit history

…(just kidding)

Verbose Version of the Steps

  1. Clone repo along with full commit history or use the “ — depth n” option to clone repo with only the last n commits. (n being a number you choose)
    Right now, you have grabbed the entire repository going only as far back as your last n commits. This creates a new folder on your computer, it’ll have the same name as your repository on Github.com.
  2. In your terminal, cd to where you want this clone to go on your computer. (Probably that new folder that was just created)
    The folder doesn’t contain anything yet.
  3. Set the desired subdirectory using its path relative to the root in the “sparse-checkout” option.
    Normally when you use the “clone” command, it’ll copy the entire repository (project w/ all commits) and you’d just use a “pull” command to execute it. Instead of pulling, you’re going to pick apart what you want by using it’s path relative to your repository’s root folder. It’ll look something like “ROOT-folder/some-subfolder/The-sub-subfolder-I-want”.
  4. “Checkout”.
    The folder you CD’d into in step 2 now contains the (sub-)subfolder that you wanted (that you named in step 3).

— depth n

“ — depth” refers to how far back(deep) into the commit history you want to go. If commit history was represented as a first-in/last-out stack of commits made in time, then “ — depth 1” refers to the most recent commit you made. Normally, when you clone your repository, you also clone it’s entire commit history. If you want to clone a part of your project from it’s current state on Github.com and you don’t care about the history of changes you’ve made, then use “ — depth 1” in your clone command.

“ — depth 1” option refers to the latest commit of your repository.
“ — depth 2” option refers to the last 2 commits of your repository.
etc. etc.
If you do not include “ — depth” in your clone command then the default number of commits that is cloned is “all of them.”

*** any criticisms or advice is very welcome and encouraged. Please comment if compelled to do so ***

--

--

JVDY

We’re all just learning. Assume it’s not the same way you do. 👍