How do you merge two Git repositories?

后端 未结 23 3244
耶瑟儿~
耶瑟儿~ 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条回答
  •  不知归路
    2020-11-21 06:00

    A single branch of another repository can be easily placed under a subdirectory retaining its history. For example:

    git subtree add --prefix=rails git://github.com/rails/rails.git master
    

    This will appear as a single commit where all files of Rails master branch are added into "rails" directory. However the commit's title contains a reference to the old history tree:

    Add 'rails/' from commit

    Where is a SHA-1 commit hash. You can still see the history, blame some changes.

    git log 
    git blame  -- README.md
    

    Note that you can't see the directory prefix from here since this is an actual old branch left intact. You should treat this like a usual file move commit: you will need an extra jump when reaching it.

    # finishes with all files added at once commit
    git log rails/README.md
    
    # then continue from original tree
    git log  -- README.md
    

    There are more complex solutions like doing this manually or rewriting the history as described in other answers.

    The git-subtree command is a part of official git-contrib, some packet managers install it by default (OS X Homebrew). But you might have to install it by yourself in addition to git.

提交回复
热议问题