How do you merge two Git repositories?

后端 未结 23 3302
耶瑟儿~
耶瑟儿~ 2020-11-21 05:45

Consider the following scenario:

I have developed a small experimental project A in its own Git repo. It has now matured, and I\'d like A to be part of larger projec

23条回答
  •  -上瘾入骨i
    2020-11-21 06:03

    If you want to merge project-a into project-b:

    cd path/to/project-b
    git remote add project-a /path/to/project-a
    git fetch project-a --tags
    git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge
    git remote remove project-a
    

    Taken from: git merge different repositories?

    This method worked pretty well for me, it's shorter and in my opinion a lot cleaner.

    In case you want to put project-a into a subdirectory, you can use git-filter-repo (filter-branch is discouraged). Run the following commands before the commands above:

    cd path/to/project-a
    git filter-repo --to-subdirectory-filter project-a
    

    An example of merging 2 big repositories, putting one of them into a subdirectory: https://gist.github.com/x-yuri/9890ab1079cf4357d6f269d073fd9731

    Note: The --allow-unrelated-histories parameter only exists since git >= 2.9. See Git - git merge Documentation / --allow-unrelated-histories

    Update: Added --tags as suggested by @jstadler in order to keep tags.

提交回复
热议问题