我在一个名为XXX的文件夹中有一个Git存储库,还有一个名为YYY的Git存储库。
我想将XXX存储库作为名为ZZZ的子目录导入到YYY存储库中,并将所有XXX的更改历史添加到YYY中。
之前的文件夹结构:
├── XXX
│ ├── .git
│ └── (project files)
└── YYY
├── .git
└── (project files)
文件夹结构后:
YYY
├── .git <-- This now contains the change history from XXX
├── ZZZ <-- This was originally XXX
│ └── (project files)
└── (project files)
这可以做到吗,或者我必须使用子模块?
根据这篇文章,使用子树对我来说是有效的,只转移了适用的历史。在这里发布,以防有人需要这些步骤(确保将占位符替换为适用于你的值):
在源存储库中将子文件夹拆分为一个新的分支
Git子树拆分——prefix=<source-path-to-merge> -b subtree-split-result
在你的目标repo合并在拆分结果分支
git remote add merge-source-repo <path-to-your-source-repository>
git fetch merge-source-repo
git merge -s ours --no-commit merge-source-repo/subtree-split-result
git read-tree --prefix=<destination-path-to-merge-into> -u merge-source-repo/subtree-split-result
验证您的更改并提交
git status
git commit
别忘了
通过删除子树拆分结果分支进行清理
git branch -D subtree-split-result
删除为从源repo获取数据而添加的远程
Git远程rm merge-source-repo
我认为你可以使用'git mv'和'git pull'来做到这一点。
我是一个公平的git新手-所以要小心你的主存储库-但我刚刚在一个临时目录中尝试了这一点,它似乎工作。
首先-重命名XXX的结构,以匹配你想要它在YYY中的样子:
cd XXX
mkdir tmp
git mv ZZZ tmp/ZZZ
git mv tmp ZZZ
现在XXX是这样的:
XXX
|- ZZZ
|- ZZZ
现在使用'git pull'来获取更改:
cd ../YYY
git pull ../XXX
现在YYY是这样的:
YYY
|- ZZZ
|- ZZZ
|- (other folders that already were in YYY)
下面是脚本,将立即工作。
#!/bin/bash -xe
# script name: merge-repo.sh
# To merge repositories into the current.
# To see the log of the new repo use 'git log --follow -- unprefixed-filename'
# So if the file is repo/test.cpp use 'git log --follow -- test.cpp'
# I'm not sure how this will work when two files have the same name.
#
# `git branch -a` will show newly created branches.
# You can delete them if you want.
merge_another() {
repo="$1" # url of the remote repo
rn="$2" # new name of the repo, you can keep the same name as well.
git remote add ${rn} ${repo}
git fetch ${rn}
git merge -s ours --no-commit --allow-unrelated-histories ${rn}/master
git read-tree --prefix=${rn}/ -u ${rn}/master
git commit -m "Imported ${rn} as a subtree."
git pull -s subtree ${rn} master
}
merge_another $1 $2
运行脚本。转到您希望合并另一个repo的repo,并运行脚本。
cd base-repo
./merge-repo.sh git@github.com:username/repo-to-be-merged.git repo-to-be-merged-new-name
现在将主分支上的更改推到remote/origin。根据您要做的事情,可能不需要此步骤。
git push origin master
在我的例子中,我只想从另一个存储库(XXX)导入一些文件。子树对我来说太复杂了,其他的解都不行。这就是我所做的:
ALL_COMMITS=$(git log --reverse --pretty=format:%H -- ZZZ | tr '\n' ' ')
这将为您提供一个以空格分隔的列表,其中包括所有影响我想要导入的文件(ZZZ)的反向顺序(您可能还必须添加——follow以捕获重命名)。然后我进入目标存储库(YYY),将另一个存储库(XXX)添加为远程,从它中获取,最后:
git cherry-pick $ALL_COMMITS
这会将所有提交添加到分支,因此您将拥有所有具有历史记录的文件,并且可以对它们做任何您想做的事情,就像它们一直在这个存储库中一样。
Git-subtree正是为这样的用例设计的脚本:将多个存储库合并为一个,同时保留历史(和/或分割子树的历史,尽管这似乎与这个问题无关)。从1.7.11版开始,它作为git树的一部分发布。
要合并一个<repo> at revision <rev> as subdirectory <prefix>的仓库,使用git子树add,如下所示:
git subtree add -P <prefix> <repo> <rev>
Git-subtree以一种更用户友好的方式实现子树合并策略。
对于您的情况,在存储库YYY中,您将运行:
git subtree add -P ZZZ /path/to/XXX.git master
缺点是在合并的历史中文件没有前缀(不在子目录中)。因此,git log ZZZ/a会显示除了合并历史之外的所有更改(如果有的话)。你可以:
git log --follow -- a
但这不会显示合并历史中其他的变化。
换句话说,如果不更改存储库XXX中的ZZZ文件,则需要指定——follow和一个无前缀路径。如果在两个存储库中都更改它们,则有两个命令,其中没有一个显示所有更改。
这里有更多。