Movatterモバイル変換


[0]ホーム

URL:


How to use the subtree merge strategy

There are situations where you want to include content in your projectfrom an independently developed project. You can just pull from theother project as long as there are no conflicting paths.

The problematic case is when there are conflicting files. Potentialcandidates are Makefiles and other standard filenames. You could mergethese files but probably you do not want to. A better solution for thisproblem can be to merge the project as its own subdirectory. This is notsupported by therecursive merge strategy, so just pulling won’t work.

What you want is thesubtree merge strategy, which helps you in such asituation.

In this example, let’s say you have the repository at/path/to/B (butit can be a URL as well, if you want). You want to merge themasterbranch of that repository to thedir-B subdirectory in your currentbranch.

Here is the command sequence you need:

$ git remote add -f Bproject /path/to/B(1)$ git merge -s ours --no-commit --allow-unrelated-histories Bproject/master(2)$ git read-tree --prefix=dir-B/ -u Bproject/master(3)$ git commit -m "Merge B project as our subdirectory"(4)$ git pull -s subtree Bproject master(5)
  1. name the other project "Bproject", and fetch.

  2. prepare for the later step to record the result as a merge.

  3. read "master" branch of Bproject to the subdirectory "dir-B".

  4. record the merge result.

  5. maintain the result with subsequent merges using "subtree"

The first four commands are used for the initial merge, while the lastone is to merge updates fromB project.

Comparingsubtree merge with submodules

  • The benefit of using subtree merge is that it requires lessadministrative burden from the users of your repository. It works witholder (before Git v1.5.2) clients and you have the code right afterclone.

  • However if you use submodules then you can choose not to transfer thesubmodule objects. This may be a problem with the subtree merge.

  • Also, in case you make changes to the other project, it is easier tosubmit changes if you just use submodules.

Additional tips

  • If you made changes to the other project in your repository, they maywant to merge from your project. This is possible using subtree — itcan shift up the paths in your tree and then they can merge only therelevant parts of your tree.

  • Please note that if the other project merges from you, then it willconnect its history to yours, which can be something they don’t wantto.

Last updated 2025-11-17 07:37:34 -0800

[8]ページ先頭

©2009-2025 Movatter.jp