合并git repositores

时间:2017-11-13 00:17:41

标签: git git-merge git-subtree

我有三个GIT存储库:

  • Repo1
  • Repo2
  • Repo3

我希望将它们统一为一个回购,就像这样:

mainRepo/Repo1

mainRepo/Repo2

mainRepo/Repo3

其中每个旧存储库都是新存储库的子文件夹。 mainRepo是一个新的存储库,里面没有任何内容。

旧的存储库将被删除,因此无需保留遥控器,我需要保留文件历史记录。我试图用子树和手动合并来做,但没有成功。

1 个答案:

答案 0 :(得分:2)

这将保留历史记录(git log --follow)。

mkdir mainRepo
cd mainRepo
git init

git remote add repo1 (path to repo1.git)
git remote add repo2 (path to repo2.git)
git remote add repo3 (path to repo3.git)
git remote update

repo1_commit=$(
    git commit-tree "$(
        printf '040000 tree %s\t%s\0' "$(git rev-parse repo1/master^{tree})" repo1 \
        | git mktree -z
    )" -p repo1/master -m 'Move repo1 to subdirectory')
repo2_commit=$(
    git commit-tree "$(
        printf '040000 tree %s\t%s\0' "$(git rev-parse repo2/master^{tree})" repo2 \
        | git mktree -z
    )" -p repo2/master -m 'Move repo2 to subdirectory')
repo3_commit=$(
    git commit-tree "$(
        printf '040000 tree %s\t%s\0' "$(git rev-parse repo3/master^{tree})" repo3 \
        | git mktree -z
    )" -p repo3/master -m 'Move repo3 to subdirectory')

git reset --hard "$(
    git commit-tree "$(
        printf '040000 tree %s\t%s\0' \
            "$(git rev-parse repo1/master^{tree})" repo1 \
            "$(git rev-parse repo2/master^{tree})" repo2 \
            "$(git rev-parse repo3/master^{tree})" repo3 \
        | git mktree -z
    )" \
    -p "${repo1_commit}" -p "${repo2_commit}" -p "${repo3_commit}" \
    -m 'Merge repo1, repo2, and repo3')"
相关问题