只将部分git库推送到第二个远程库



我有一个现有的git存储库(例如在我的组织的私有存储库中),由几个子项目组成,组织在特定的文件夹中。它有一种方法克隆每个这些子项目到特定的第二个远程存储库(例如在github上)?我知道git子树似乎是方便的另一种方式(有现有的子项目作为一个新的整体项目的依赖)。然而,如果我理解正确的话,使用subtree需要我从现有的整体项目中删除子项目,并再次将它们作为子树添加。

要求能够将整个项目(包括其子项目)推送到私有组织的远程存储库,以在那里实现工作版本。

你读过子树文档了吗?

split [<local-commit>]
Extract a new, synthetic project history from the history of the <prefix> subtree of <local-commit>,
or of HEAD if no <local-commit> is given. The new history includes only the commits (including
merges) that affected <prefix>, and each of those commits now  has the contents of <prefix> at the
root of the project instead of in a subdirectory. Thus, the newly created history is suitable for
export as a separate git repository.

听起来像你想要的。

这是一个工作的例子;

初始化演示库;

mkdir subtree-split
cd subtree-split/
git init
git commit -m "Init." --allow-empty

为项目A添加一些内容;

mkdir A
touch A/README.txt
git add A/
git commit -m "Readme for A."

为项目B添加一些内容;

mkdir B
touch B/README.txt
git add B/
git commit -m "Readme for B."

将B拆分为新的子树;

git subtree --prefix=B/ split 

创建一个新的存储库,只将B的历史记录推送到;

cd ..
mkdir subtree-B
cd subtree-B
git init
git commit -m "Init subtree-B." --allow-empty

将子树B历史推送到远程;

cd ../subtree-split/
git subtree --prefix B/ push ../subtree-B HEAD

检查B的病史;

cd ../subtree-B
git log --all  --pretty=oneline --abbrev-commit
a26e5de (HEAD -> master) Init subtree-B.
0523c62 (HEAD) Readme for B.

无A病史!

最新更新