How to merge nested git repo into parent repo, retaining history?

前端 未结 4 1527
天命终不由人
天命终不由人 2020-12-31 18:42

Now I know most git experts will immediately think of git rebase, but I am using the word \"rebase\" in the more general sense: I have the following structure o

4条回答
  •  失恋的感觉
    2020-12-31 19:20

    Quick and easy way:

    Rename all files in src, so they will start with src/. Add src repo as remote, fetch & merge. Drop old src repo, everything is now in ./.

    This will leave you with this action recorded in history.

    History rewrite:

    To make this merge invisible, you need to use git filter-branch --tree-filter to add src/ prefix in src repository. Then add this as a remote to ./ repository and fetch it (no merge yet). To blend history nicely, you need to reorder commits. Use git log --date-order master src/master to retrieve these commits in correct order and cherry-pick them:

    git checkout -b new-master your-first-commit
    git log --format='%H' --date-order --reverse master src/master | xargs git cherry-pick
    

    This basically does merge sort on your commits and lines them up to linear history.

    This is will not preserve your merges, so dont do it unless you have flat history. In that case use only filter-branch and then do normal merge.

提交回复
热议问题