如何使用git-filter-repo将一个仓库作为子目录合并到另一个仓库

时间:2020-04-25 03:37:50

标签: git github git-filter-branch git-filter-repo

在尝试使用git filter-branch来合并使用此gist的存储库时。仅添加相关的代码段:

git filter-branch -f --prune-empty --tree-filter '
        mkdir -p "${REPO_NAME}_tmp"
        git ls-tree --name-only $GIT_COMMIT | xargs -I{} mv {} "${REPO_NAME}_tmp"
        mv "${REPO_NAME}_tmp" "$REPO_NAME"
    '

我从git收到警告,内容如下:

WARNING: git-filter-branch has a glut of gotchas generating mangled history
     rewrites.  Hit Ctrl-C before proceeding to abort, then use an
     alternative filtering tool such as 'git filter-repo'
     (https://github.com/newren/git-filter-repo/) instead.  See the
     filter-branch manual page for more details; to squelch this warning,
     set FILTER_BRANCH_SQUELCH_WARNING=1.

因此,我看了一下git filter-repodescription指出:

重组文件布局(例如将所有文件移动到子目录中,以准备与另一个仓库合并...

这表明可以使用git filter-repo来解决此问题,但是即使在检查了文档并给出examples之后,我也找不到解决该问题的方法。有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

在脚本中替换filter-branch命令

git filter-branch -f --prune-empty --tree-filter '
        mkdir -p "${REPO_NAME}_tmp"
        git ls-tree --name-only $GIT_COMMIT | xargs -I{} mv {} "${REPO_NAME}_tmp"
        mv "${REPO_NAME}_tmp" "$REPO_NAME"
    '

与此

git filter-repo --to-subdirectory-filter "$REPO_NAME"

请参见路径快捷方式部分here

-至子目录过滤器<directory>

将项目根目录视为<directory>

等效于使用--path-rename :<directory>/

答案 1 :(得分:1)

这看起来像是路径重命名,是path-based filters的一部分:

cd  $REPO_DIR_TMP
git filter-repo  --path-rename /:${REPO_NAME}_tmp/

这将重写子文件夹(在该第二个存储库内)中第二个存储库的历史记录

然后,您可以像在gitst中一样,将其添加为第一个回购,获取和合并的远程版本。

相关问题