How to Clone Subdirectory of Git Repository

Azhar Bashir Khan Feb 02, 2024
How to Clone Subdirectory of Git Repository

This tutorial will teach how to clone a subdirectory of a Git repository.

Git, a version control system, maintains the history of the changes done to a project directory. In a typical project development environment, one would segregate different project modules into different subdirectories.

One then can only check out or clone specific project module subdirectory. We can use the sparse-checkout feature provided by Git for such purposes.

We will now illustrate this with an example.

Clone or Checkout Subdirectory in the Repository in Git

Git is used in a collaborative development environment to keep track of the changes done to files in the project directory. One would keep different project modules in separate subdirectories inside the main project directory in the project development environment.

The Git repository would track this main project directory, and thus one can clone or check out the entire project directory. Typically, different teams would work on different project modules in a large project.

Thus, in such cases, one would not require to check out the entire project directory. It would suffice to only clone the project module subdirectory that a particular team is working on.

Cloning only specific subdirectories (i.e.) a subset of the project Git repository is called sparse checkout. We can checkout only the subdirectories we want from the project directory in the Git repository in a sparse checkout.

Suppose we have a project directory named my_project. In the project directory my_project, there are subdirectories for different modules named frontend, backend, documentation, etc.

We only want to checkout or clone the frontend module subdirectory. Thus, we will now first create a directory for the repository, as follows.

$ mkdir my_project
$ cd my_project

After creating the project directory, we will now initialize the Git repository and add the remote url, as follows.

$ git init
$ git remote add -f origin https://github.com/johndoe/my_project.git

We must enable sparse checkouts to only checkout or clone subdirectories of the main project directory.

We can use the git config command to achieve the same. We need to use the git config command as follows.

$ git config core.sparsecheckout true

We can now tell Git which subdirectories we want to check out.

Thus, to only enable checkout of the frontend subdirectory, we need to list it in the .git/info/sparse-checkout file.

$ echo "frontend/" >> .git/info/sparse-checkout

Now, we can fetch the files from the remote Git repository.

$ git pull origin master

Now, we have in the working tree only the frontend subdirectory of the main project, my_project.

Thus, we have learned how to clone or checkout only specific subdirectories of a Git repository in Git.

For more information, please visit:

  1. git-init
  2. git-read-tree
  3. git-sparse-checkout

Related Article - Git Clone