About Git subtree merges
source link: https://docs.github.com/en/get-started/using-git/about-git-subtree-merges
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
About subtree merges
Typically, a subtree merge is used to contain a repository within a repository. The "subrepository" is stored in a folder of the main repository.
The best way to explain subtree merges is to show by example. We will:
- Make an empty repository called
test
that represents our project - Merge another repository into it as a subtree called
Spoon-Knife
. - The
test
project will use that subproject as if it were part of the same repository. - Fetch updates from
Spoon-Knife
into ourtest
project.
Setting up the empty repository for a subtree merge
- Open Terminal.
- Create a new directory and navigate to it.
$ mkdir test $ cd test
- Initialize a new Git repository.
$ git init > Initialized empty Git repository in /Users/octocat/tmp/test/.git/
- Create and commit a new file.
$ touch .gitignore $ git add .gitignore $ git commit -m "initial commit" > [main (root-commit) 3146c2a] initial commit > 0 files changed, 0 insertions(+), 0 deletions(-) > create mode 100644 .gitignore
Adding a new repository as a subtree
-
Add a new remote URL pointing to the separate project that we're interested in.
$ git remote add -f spoon-knife https://github.com/octocat/Spoon-Knife.git > Updating spoon-knife > warning: no common commits > remote: Counting objects: 1732, done. > remote: Compressing objects: 100% (750/750), done. > remote: Total 1732 (delta 1086), reused 1558 (delta 967) > Receiving objects: 100% (1732/1732), 528.19 KiB | 621 KiB/s, done. > Resolving deltas: 100% (1086/1086), done. > From https://github.com/octocat/Spoon-Knife > * [new branch] main -> Spoon-Knife/main
-
Merge the
Spoon-Knife
project into the local Git project. This doesn't change any of your files locally, but it does prepare Git for the next step.If you're using Git 2.9 or above:
$ git merge -s ours --no-commit --allow-unrelated-histories spoon-knife/main > Automatic merge went well; stopped before committing as requested
If you're using Git 2.8 or below:
$ git merge -s ours --no-commit spoon-knife/main > Automatic merge went well; stopped before committing as requested
-
Create a new directory called spoon-knife, and copy the Git history of the
Spoon-Knife
project into it.$ git read-tree --prefix=spoon-knife/ -u spoon-knife/main
-
Commit the changes to keep them safe.
$ git commit -m "Subtree merged in spoon-knife" > [main fe0ca25] Subtree merged in spoon-knife
Although we've only added one subproject, any number of subprojects can be incorporated into a Git repository.
Tip: If you create a fresh clone of the repository in the future, the remotes you've added will not be created for you. You will have to add them again using the git remote add
command.
Synchronizing with updates and changes
When a subproject is added, it is not automatically kept in sync with the upstream changes. You will need to update the subproject with the following command:
$ git pull -s subtree REMOTE-NAME BRANCH-NAME
For the example above, this would be:
$ git pull -s subtree spoon-knife main
Further reading
Recommend
-
25
-
15
Optimising The Performance Of Power Query Merges In Power BI, Part 5: Cross Joins In a late addition to the series of posts that I started
-
6
Optimising The Performance Of Power Query Merges In Power BI, Part 4: Table.Join And Other Join Algorithms In the
-
2
Gojek merges with Tokopedia to create ride-hailing and e-commerce behemothGojek merges with Tokopedia to create ride-hailing and e-commerce behemoth May 19, 2021 11:10 am Indonesia ride-hailing and payments firm Gojek has announced i...
-
2
Instagram Retires IGTV Brand, Merges Video Feed Posts into a Single Format Published Oct. 5, 2021 By
-
5
Git Subtree 的使用 17 October 2021 浏览量:5 git 的 subtree 是一种复用源代码的方式,可以让多个仓库引用某个仓库的代码,也可以将仓库中的某个目录拆分成一个子仓库以供其他仓库使用。
-
8
News » News & Analysis » Singapore tokenisation startup Trovio merges with Sydney digital...
-
3
Git merges conflict resets everything advertisements I had to roll back the Live server and now I'm getting con...
-
1
Git Subtree Basics If you hate git submodule, then you may want to give git subtree a try. Background When you want to use a subtree, you add the subtree to an existing repository where t...
-
7
将git仓库从submodule转换为subtree
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK