且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何组合两个不相关的git存储库,保存历史记录

更新时间:2023-02-09 21:55:06

While @codeWizard's reply was helpful, that approach didn't retain the timestamps the way I wanted. It did lead me down a rabbit hole that helped me find a solution though...

  1. Create a new, blank repository

    git init
    

  2. Add and fetch the old repositories as remotes

    git remote add -f oldRepoA ../oldRepoA
    git remote add -f oldRepoB ../oldRepoB
    

  3. Export the combined commit history by timestamp and hash, pipe the output to sort, discard the timestamps via cut, and then pipe the list of chronologically sorted hashes to xargs, which runs a shell script to export a patch for each individual hash and then immediately apply the patch to the new repo.

    git log --all --oneline --format="%at %H" | sort | cut -c12- | 
        xargs -I {} sh -c 
            'git format-patch -1 {} --stdout | 
             git am --committer-date-is-author-date'
    

The --committer-date-is-author-date is key to keeping the original timestamps. There might be a better way of doing this, but this works well enough for my use case!

相关阅读

技术问答最新文章