git合并子树

    很多时候 ,"子模块"(submodule)功能不能满足手边的任务。例如,合并多个仓库到一个仓库时,要维护每一个仓库的历史记录。对于这种情况,子树(subtree)合并策略是一个更好的解决方案。

建立仓库,完成第一个合并

在这个例子中,我们建立一个空的“父”仓库,合并别的仓库作为它的子路径。

首先,建立一个空的仓库:

$ mkdir test
$ cd test
$ git init
# Initialized empty Git repository in /Users/tekkub/tmp/test/.git/
$ touch .gitignore
$ git add .gitignore
$ git commit -m "initial commit"
# [master (root-commit) 3146c2a] initial commit
#  0 files changed, 0 insertions(+), 0 deletions(-)
#  create mode 100644 .gitignore

现在,子树合并tekkub/cork 到仓库的/cork目录下:

$ git remote add -f cork git://github.com/TekNoLogic/Cork.git
# Updating cork
# 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 git://github.com/tekkub/cork
#  * [new branch]      lastbuffed -> cork/lastbuffed
#  * [new branch]      lock_n_mount -> cork/lock_n_mount
#  * [new branch]      master     -> cork/master
#  * [new branch]      nothing_to_see_here -> cork/nothing_to_see_here

$ git merge -s ours --no-commit cork/master
# Automatic merge went well; stopped before committing as requested

接下来,我们合并tekkub/panda到仓库的panda/目录下

$ git remote add -f panda git://github.com/TekNoLogic/Panda.git
# Updating panda
# warning: no common commits
# remote: Counting objects: 974, done.
# remote: Compressing objects: 100% (722/722), done.
# remote: Total 974 (delta 616), reused 399 (delta 251)
# Receiving objects: 100% (974/974), 189.56 KiB, done.
# Resolving deltas: 100% (616/616), done.
# From git://github.com/tekkub/panda
#  * [new branch]      master     -> panda/master
#  * [new branch]      transmute  -> panda/transmute

$ git merge -s ours --no-commit panda/master
# Automatic merge went well; stopped before committing as requested

$ git read-tree --prefix=panda/ -u panda/master
$ git commit -m "Subtree merged in panda"
# [master 726a2cd] Subtree merged in panda

最后,我们将要把tekkub/cork的子目录modules/合并到仓库的cork2/目录结构下。

$ git merge -s ours --no-commit cork/master
# Automatic merge went well; stopped before committing as requested

$ git read-tree --prefix=cork2/ -u cork/master:modules
$ git commit -m "Subtree merged in cork/modules"
# [master f240057] Subtree merged in cork/modules

目录结构为

Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2013/3/13 12:45 cork
d---- 2013/3/13 12:50 cork2
d---- 2013/3/13 12:48 panda
----- 2013/3/13 12:36 0 .gitignore

 

获取修改

如果将来被合并的仓库中有修改,你只要简单的使用-s subtree标记获取他们的修改。

$ git pull -s subtree panda master

原文链接: https://help.github.com/articles/working-with-subtree-merge

原文地址:https://www.cnblogs.com/Mingxx/p/2957483.html