git

Merge git repo into branch of another repo


Given repo Foo and repo Bar. I want to merge Bar with Foo, but only into a separate branch, called baz.

git switch -c baz <= put the Bar repo here.


Solution

  • You can't merge a repository into a branch. You can merge a branch from another repository into a branch in your local repository.

    Assuming that you have two repositories, base-repo and other-repo, where you want to merge other-repo into base-repo:

    Change into the base-repo repository:

    $ cd base-repo
    

    Add the other-repo repository as a remote and fetch it:

    $ git remote add other-repo git@github.com:xxx/other-repo.git # or use a path like ../foo if you have it locally on your machine
    $ git remote update
    

    Create a new branch base-with-other in the base-repo based on whatever your current branch is:

    $ git switch -c base-with-other # choose any branch name you want and put it after -c
    

    Merge branch somebranch from the other-repo into the current branch:

    $ git merge --allow-unrelated-histories other-repo/somebranch
    

    (--allow-unrelated-histories is not required prior to git version 2.9)