【问题标题】:Push only part of git repository to second remote repository仅将部分 git 存储库推送到第二个远程存储库
【发布时间】:2021-11-17 22:19:35
【问题描述】:

我有一个现有的 git 存储库(例如,在我的组织的私有存储库中),其中包含组织在特定文件夹中的多个子项目。有没有办法将这些子项目中的每一个克隆到特定的第二个远程存储库(例如在 github 上)?我知道 git 子树似乎反过来很方便(将现有子项目作为新的整体项目中的依赖项)。但是,如果我理解正确,使用子树需要我从现有的整体项目中删除子项目并再次将它们添加为子树。

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

【问题讨论】:

  • 简短的回答是“否”(因为 Git 推送 commits,而不是文件),但长的答案是“否”和“是”(因为您当然可以制作 其他 提交;这就是git subtree split 的意义所在,如Adam answers)。 git subtree 中有已知的错误,但我认为它们中的任何一个都不会影响基本的拆分使用。
  • 很高兴见到你@torek o/

标签: git git-subtree


【解决方案1】:

您阅读过子树文档吗?

  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 的历史!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多