Table of Contents
Setting up the remote
Merging the subtree
Updating the subtree
Home Development Tools git How do I add a subtree to my Git repository?

How do I add a subtree to my Git repository?

Jul 16, 2025 am 01:48 AM
git subtree

To add a subtree to a Git repository, first add the remote repository and get its history, then merge it into a subdirectory using the git merge and git read-tree commands. The steps are as follows: 1. Use the git remote add -f command to add a remote repository; 2. Run git merge --s recursive --no-commit to get branch content; 3. Use git read-tree --prefix= to specify the directory to merge the project as a subtree; 4. Submit changes to complete the addition; 5. When updating, first git fetch and repeat the merging and steps to submit the update. This method keeps the external project history complete and easy to maintain.

How do I add a subtree to my Git repository?

Adding a subtree to your Git repository is a useful way to include another project as a subdirectory inside your main repo, while still keeping its history intact. It's especially handy when you want to pull in third-party libraries or share code between projects without using submodules. Here's how to do it effectively.


Setting up the remote

Before adding a subtree, you need to have the remote repository set up. If you haven't already added it as a remote, now's the time to do so. This makes it easier to update later.

You can add a new remote like this:

 git remote add -f <remote-name> <repository-url>

For example:

 git remote add -f vendor https://github.com/example/external-project.git

The -f flag fetches the remote right away, which saves you a step. Once that's done, you're ready to actually merge it into your project.


Merging the subtree

Now comes the actual process of merging the external repo as a subtree. You'll use git merge with the --s recursive and --no-commit flags to avoid conflicts and keep things flexible.

Here's the basic command:

 git merge --s recursive --no-commit <remote-name>/<branch-name>

Then tell Git that this is a subtree:

 git read-tree --prefix=<subdir-name>/ -u <remote-name>/<branch-name>

So if you were adding the vendor remote's main branch into a folder called lib/external , it would look like:

 git merge --s recursive --no-commit vendor/main
git read-tree --prefix=lib/external -u vendor/main

Finally, commit the changes:

 git commit -m "Added external project as subtree"

This will bring the entire project into your repo under the specified directory.


Updating the subtree

One of the nice things about subtrees is that you can update them easily. When the external project gets new commits, just pull those changes into your subtree:

  1. Fetch the latest changes from the remote:

     git fetch vendor
  2. Merge the updates using the same subtree logic:

     git merge --s recursive --no-commit vendor/main
    git read-tree --prefix=lib/external -u vendor/main
    git commit -m "Updated external subtree"

    This keeps your embedded project current without having to manually copy files.


    That's basically it. Once you've got the hang of it, working with subtrees becomes second nature — it's not complicated, but it's easy to overlook small steps like fetching first or using the correct prefix. Keep a quick reference handy until it clicks.

    The above is the detailed content of How do I add a subtree to my Git repository?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install numpy library in python three ways to install numpy library in python How to install numpy library in python three ways to install numpy library in python May 28, 2025 pm 04:03 PM

There are three ways to install the NumPy library: 1. Use pip to install: pipinstallnumpy, which is simple but may encounter permissions or network problems; 2. Use conda to install: condainstallnumpy, which is suitable for Anaconda environment, and automatically resolves dependencies; 3. Install: gitclone from source code and compile, which is suitable for special needs but complicated processes.

Performance Tuning of Jenkins Deployment on Debian Performance Tuning of Jenkins Deployment on Debian May 28, 2025 pm 04:51 PM

Deploying and tuning Jenkins on Debian is a process involving multiple steps, including installation, configuration, plug-in management, and performance optimization. Here is a detailed guide to help you achieve efficient Jenkins deployment. Installing Jenkins First, make sure your system has a Java environment installed. Jenkins requires a Java runtime environment (JRE) to run properly. sudoaptupdatesudoaptininstallopenjdk-11-jdk Verify that Java installation is successful: java-version Next, add J

Create and manage multiple project workspaces in VSCode Create and manage multiple project workspaces in VSCode May 29, 2025 pm 10:09 PM

Create and manage multiple project workspaces in VSCode through the following steps: 1. Click the "Manage" button in the lower left corner, select "New Workspace", and decide the save location. 2. Give the workspace a meaningful name, such as "WebDev" or "Backend". 3. Switch the project in Explorer. 4. Use the .code-workspace file to configure multiple projects and settings. 5. Pay attention to version control and dependency management to ensure that each project has .gitignore and package.json files. 6. Clean useless files regularly and consider using remote development skills

Solve the layout settings and display problems of VSCode in multi-screen environment Solve the layout settings and display problems of VSCode in multi-screen environment May 29, 2025 pm 10:12 PM

Using VSCode in a multi-screen environment can solve layout and display problems by adjusting the window size and position, setting workspaces, adjusting interface scaling, rationally laying tool windows, updating software and extensions, optimizing performance, and saving layout configuration, thereby improving development efficiency.

How to create Laravel package (Package) development? How to create Laravel package (Package) development? May 29, 2025 pm 09:12 PM

The steps to create a package in Laravel include: 1) Understanding the advantages of packages, such as modularity and reuse; 2) following Laravel naming and structural specifications; 3) creating a service provider using artisan command; 4) publishing configuration files correctly; 5) managing version control and publishing to Packagist; 6) performing rigorous testing; 7) writing detailed documentation; 8) ensuring compatibility with different Laravel versions.

Analysis of VSCode's support trends and related issues for emerging programming languages Analysis of VSCode's support trends and related issues for emerging programming languages May 29, 2025 pm 10:06 PM

VSCode's support trend for emerging programming languages ​​is positive, mainly reflected in syntax highlighting, intelligent code completion, debugging support and version control integration. Despite scaling quality and performance issues, they can be addressed by choosing high-quality scaling, optimizing configurations, and actively participating in community contributions.

Troubleshooting data consistency issues when PHP operates MySQL database Troubleshooting data consistency issues when PHP operates MySQL database May 28, 2025 pm 06:12 PM

To troubleshoot data consistency issues when PHP operates MySQL databases, you need to start with transaction management, code logic, and database configuration. 1. Use STARTTRANSACTION and COMMIT/ROLLBACK to ensure transaction integrity. 2. Check the code logic to avoid variable errors. 3. Set appropriate MySQL isolation level such as REPEATABLEREAD. 4. Use ORM tools to simplify transaction management. 5. Check PHP and MySQL log location issues. 6. Use the version control system to manage database change scripts.

The reasons and solutions for editor crash after VSCode plug-in update The reasons and solutions for editor crash after VSCode plug-in update May 29, 2025 pm 10:03 PM

The reason why the editor crashes after the VSCode plugin is updated is that there is compatibility issues with the plugin with existing versions of VSCode or other plugins. Solutions include: 1. Disable the plug-in to troubleshoot problems one by one; 2. Downgrade the problem plug-in to the previous version; 3. Find alternative plug-ins; 4. Keep VSCode and plug-in updated and conduct sufficient testing; 5. Set up automatic backup function to prevent data loss.

See all articles