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

前端 未结 4 1512
天命终不由人
天命终不由人 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:13

    Use a subtree merge.

    I would list out the steps, but they are in the answer to this question: How do you merge two git repositories?

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-31 19:20

    No really use if this would work but could be worth a try. Assuming that the src is on a remote server and the parent is on a remote server. You can try the following.

    • git clone url to remote server to clone the the repo with tools, lib, and build
    • git add remote src url to the src remote repo
    • create a tracking branch of the src remote repo and then check it out on the parent repo
    • merge remote tracking branch into you main branch.
    • delete the second remote.
    0 讨论(0)
  • 2020-12-31 19:22

    I needed to do the same thing and this worked for me:

    From the parent repo,

    git remote add -f subrepo git@github.com:sub/repo.git
    git merge -s ours --no-commit subrepo/master
    git read-tree --prefix=subrepo/ -u subrepo/master
    git commit -m "Subtree merged"
    

    This is from a GitHub post that also has more detail: https://help.github.com/articles/about-git-subtree-merges/

    0 讨论(0)
提交回复
热议问题