我想从具有不同目录结构的不同repo中挑选一些文件和更改,而不是复制内容并保留历史记录。它可以用吉特吗?
如"将文件从一个Git存储库移动到另一个,保留历史记录"中所述,如果要导入的文件位于文件夹中,则这会更容易:导出/导入文件夹(及其历史记录)比仅导入文件更容易。
目标:将目录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
- pull同时复制文件和历史记录
步骤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远程rm报告分支
您有本答案或本答案中描述的其他选项,这些选项保留完整路径名(但强制您指定要排除的所有选项,这比指定要保留的一个文件夹要长)。
# 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