JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Clone a subdirectory of a Git repository

Author:JIYIK Last Updated:2025/04/21 Views:

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

Git is a version control system that maintains a history of changes made to a project directory. In a typical project development environment, people separate different project modules into different subdirectories.

Then only specific project module subdirectories can be checked out or cloned. To do this, we can use sparse-checkoutthe functionality provided by Git.

We will now illustrate this with an example.


Clone or checkout a subdirectory in a Git repository

Git is used in collaborative development environments to track changes made to files in a project directory. One approach is to keep different project modules in different subdirectories within the main project directory of the project development environment.

The Git repository will track this main project directory, so the entire project directory can be cloned or checked out. Often, different teams will work on different project modules within a large project.

So, in this case, there is no need to check in the entire project directory. It is enough to clone only the project module subdirectory that a specific team is working on.

Cloning only a specific subdirectory (i.e. a subset of) a project Git repository is called a sparse checkout. We can do this by sparse checkoutchecking out only the subdirectory we want from the project directory in the Git repository.

Assume we have a my_projectproject directory called . In the project directory my_project, there are subdirectories for different modules, named frontend, backend, and documentationso on.

We only want to checkout or clone frontendthe modules subdirectory. Therefore, we will now first create a directory for the repository as shown below.

$ mkdir my_project
$ cd my_project

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

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

We have to enable to sparse checkoutscheckout or clone only subdirectories of the main project directory.

We can git configachieve the same using the command. We need to use git configthe command as shown below.

$ git config core.sparsecheckout true

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

frontendTherefore, to enable checkout of only the subdirectory, we need to .git/info/sparse-checkoutlist it in the file.

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

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

$ git pull origin master

my_projectNow, we only have the main project 's subdirectory in our working tree frontend.

So, we learned how to clone or checkout only a specific subdirectory of a Git repository in Git.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Run a batch (.bat) file in CMD

Publish Date:2025/04/21 Views:169 Category:OPERATING SYSTEM

This article will show you how to use CMD to run a batch file.bat. There are three ways in which you can run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way yo

Running batch scripts using Task Scheduler

Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM

This article will show you how to use Task Scheduler to run a batch file. Running batch scripts using Task Scheduler With Task Scheduler, you can automate tasks to run automatically at specific times. It only takes a few steps and you don't

Solve the error Make Command Not Found in Cygwin

Publish Date:2025/04/21 Views:75 Category:OPERATING SYSTEM

Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err

Difference between Bash Nohup and &

Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM

This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux processes in the backgr

Bash Nohup 与 & 的区别

Publish Date:2025/04/21 Views:186 Category:OPERATING SYSTEM

这篇简短的文章介绍了通过 Bash 在后台运行 Linux 进程的 nohup 命令和 控制运算符。 此外,我们将进一步研究 nohup 和 之间的主要区别。 在后台运行 Linux 进程 Linux 提供了两种在后台运行

Getting Timestamp in Bash

Publish Date:2025/04/21 Views:147 Category:OPERATING SYSTEM

This article discusses the date Bash command used to obtain the system date/time and UNIX timestamp. Get Timestamp Using date Command in Bash The Linux terminal uses the date command to print the current date and time. The simplest version

Pretty Printing JSON in Shell Script

Publish Date:2025/04/21 Views:145 Category:OPERATING SYSTEM

JSON is a textual method for representing JavaScript object literals and arrays and scalar data. It is relatively easier to read and write, and easier for manageable software to parse and generate. JSON is commonly used to serialize structu

Find the current folder name in Bash

Publish Date:2025/04/21 Views:189 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial