且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Git子树-与贡献者共享子树

更新时间:2023-01-17 18:36:54

使用git subtree pull.

git subtree add将另一个存储库的历史记录添加为 new 子树-在您的情况下,这是由您的同事完成的,因此它将不起作用

git subtree add adds a history of another repository as a new subtree -- in your case, this has been done by your colleague, so it won't work

git subtree merge将更改合并到给定的 local 提交到子树中-由于您尚未撤消这些更改,因此也不起作用

git subtree merge merges the changes up to a given local commit into the subtree -- as you have not pulled those changes yet, it won't work either

git subtree pull从另一个存储库中提取更改,然后将其合并到子树中.使用以下命令序列进行了测试(使用子树存储库的主"分支,如有需要,进行调整).

git subtree pull pulls the changes from another repo and then merges them into the subtree. Tested with the following command sequence (uses the 'master' branch of the subtree repository, adjust if needed).


~ $ mkdir subrepo
~ $ cd subrepo
~/subrepo $ git init
Initialized empty Git repository in ~/subrepo/.git/
~/subrepo $ touch file
~/subrepo $ git add file
[master (root-commit) 03a9f75] file added
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file
~/subrepo $ cd ..
~ $ mkdir repo1
~ $ cd repo1
~/repo1 $ git init
Initialized empty Git repository in ~/repo1/.git/
~/repo1 $ git commit --allow-empty -m "initial commit"
[master (root-commit) ec7e1c5] initial commit
~/repo1 $ git subtree add --prefix=sub ../subrepo master
git fetch ../subrepo master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../subrepo
 * branch            master     -> FETCH_HEAD
Added dir 'sub'
~/repo1 $ cd ..
~ $ git clone repo1 repo2
~ $ cd subrepo
~/subrepo $ echo 'new version' > file
~/subrepo $ git add file
~/subrepo $ git commit -m "file changed"
[master c72ac6e] file changed
 1 file changed, 1 insertion(+)
~/subrepo $ cd ../repo2
~/repo2 $ git subtree pull --prefix=sub ../subrepo master
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../subrepo
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 sub/file | 1 +
 1 file changed, 1 insertion(+)
~/repo2 $ cat sub/file
new version