Git merge submodule into parent tree cleanly and preserving commit history

前端 未结 2 1179
南方客
南方客 2020-12-24 13:13

I have a repository with two submodules that I want to convert into a single project. Many answers involve scripts, and some seem to be overcomplicated.

[sub         


        
2条回答
  •  春和景丽
    2020-12-24 13:56

    A bit late to the party, but for those still looking for help, checkout these:

    1. https://gitirc.eu/howto/using-merge-subtree.html
    2. https://help.github.com/en/github/using-git/about-git-subtree-merges

    Below is a somewhat verbatim copy the first post:

    1. git remote add -f Bproject /path/to/B
    2. git merge -s ours --no-commit --allow-unrelated-histories Bproject/master
    3. git read-tree --prefix=dir-B/ -u Bproject/master
    4. git commit -m "Merge B project as our subdirectory"
    
    5. git pull -s subtree Bproject master
    

    The explanation

    1. name the other project "Bproject", and fetch.
    2. prepare for the later step to record the result as a merge.
    3. read "master" branch of Bproject to the subdirectory "dir-B".
    4. record the merge result.
    
    5. pull in subsequent update from Bproject using "subtree" merge
    

    As an alternative before step 4, you might want to update .gitmodules file, or just remove it:

    3.1 git rm --cached .gitmodules
    

    With this the history from the submodule is well preserved.

提交回复
热议问题