【问题标题】:GIT: Is it possible to pick files or changes from a different repo with different directory structureGIT:是否可以从具有不同目录结构的不同存储库中选择文件或更改
【发布时间】:2020-05-11 13:13:57
【问题描述】:

我想从具有不同目录结构的不同存储库中选择一些文件和更改,而不是复制内容并保留历史记录。 git里面可以吗?

【问题讨论】:

  • 你不能简单地使用文件系统复制/cp,然后git添加新文件吗?

标签: git


【解决方案1】:

如“Moving Files from one Git Repository to Another, Preserving History”中所述,如果您要导入的文件位于文件夹中,这会更容易:导出/导入文件夹(及其历史记录)比仅文件更容易。

目标:将目录 1 从 Git 存储库 A 移动到 Git 存储库 B。

  • 制作存储库 A 的副本,这样您就可以处理它而不必过多担心错误。
  • 删除指向原始存储库的链接也是一个好主意,以避免意外进行任何远程更改(第 3 行)。
  • 第 4 行是这里的关键步骤。它会遍历您的历史记录和文件,删除不在目录 1 中的所有内容

即:

git clone <git repository A url>
cd <git repository A directory>
git remote rm origin
git filter-branch --subdirectory-filter <directory 1> -- --all
mkdir <directory 1>
mv * <directory 1>
git add .
git commit
  • 如果您还没有存储库 B,请制作一份副本。
  • 在第 3 行,您将创建到存储库 A 的远程连接,作为存储库 B 中的分支。
  • 然后简单地从这个分支(仅包含您要移动的目录)拉入存储库 B。
  • 拉取复制文件和历史记录。

第 2 步:

git clone <git repository B url>
cd <git repository B directory>
git remote add repo-A-branch <git repository A directory>
git pull repo-A-branch master --allow-unrelated-histories

git remote rm repo-A-branch

您还有this answerthis one 中描述的其他选项,它们会保留完整的路径名(但会强制您指定要排除的所有内容,这比指定要保留的一个文件夹要长)。

# 1. clone the source
git clone ssh://<user>@<source-repo url>
cd <source-repo>
# 2. remove the stuff we want to exclude
git filter-branch --tree-filter "rm -rf <files to exclude>" --prune-empty HEAD
# 3. move to target repo and create a merge branch (for safety)
cd <path to target-repo>
git checkout -b <merge branch>
# 4. Add the source-repo as remote 
git remote add source-repo <path to source-repo>
# 5. fetch it
git pull source-repo master

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多