Git子树-与贡献者共享子树 [英] Git subtree -- sharing subtrees with contributors

查看:105
本文介绍了Git子树-与贡献者共享子树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在使用的某些存储库中,我发现git子树在共享存储库时的行为不符合预期.

In some of the repositories that I'm working with, I found git subtree does not behave as expected when sharing repos.

这是场景:

  1. 我拥有主仓库X并将其推到原处.
  2. 我的同事从源中拉出仓库X,添加指向另一个仓库Y的子树,进行提交,然后将X推回源.
  3. 我拉X并查看同事所做的更改.但是,在我的本地存储库中,包含子树Y的文件夹只是另一个文件.即使将回购Y添加为远程,我也无法从回购Y中提取更改. Git子树总是说我执行git subtree add时该文件夹已经存在,并且说当我执行git subtree merge时它找不到提交.
  1. I own main repo X and push it to origin.
  2. My colleague pulls repo X from origin, adds a subtree pointing to another repo Y, makes the commit, and pushes X back to origin.
  3. I pull X and see the changes my colleague has made. However, in my local repo, the folder containing subtree Y is just another file. I can't pull changes from repo Y even after adding repo Y as a remote. Git subtree always says that the folder already exists whenever I do a git subtree add and says that it can not find the commit when I do git subtree merge.

有人知道应对这种情况的最佳方法吗?理想情况下,即使我不是用户将存储库Y添加为存储库X的子树,我也希望能够从存储库Y中提取更改.这可能吗?如果没有,那么共享具有子树的存储库的最佳方法是什么?

Does anyone know the best way to deal with this situation? Ideally I would like to also be able to pull changes from repo Y even though I am not the user adding repo Y as a subtree of repo X. Is this possible? If not, what is the best way to share repositories having subtrees?

推荐答案

使用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

这篇关于Git子树-与贡献者共享子树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆