174

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.

Mike Williamson
  • 4,801
  • 11
  • 60
  • 93
Milktrader
  • 8,660
  • 12
  • 49
  • 68
  • If you have homebrew `hub` command installed, you can do `hub merge `. But it fails until you do `git remote add upstream .git` before hand. I don't know if remote name needs to be upstream. – Devin Rhode May 24 '21 at 22:43

3 Answers3

305

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, foo and bar both located in your current directory:

$ ls
foo bar

Change into the foo repository:

$ cd foo

Add the bar repository as a remote and fetch it:

$ git remote add bar ../bar
$ git remote update

Create a new branch baz in the foo repository based on whatever your current branch is:

$ git switch -c baz

Merge branch somebranch from the bar repository into the current branch:

$ git merge --allow-unrelated-histories bar/somebranch

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

Community
  • 1
  • 1
larsks
  • 228,688
  • 37
  • 348
  • 338
  • 24
    since git 2.9 you'll probably need to add `--allow-unrelated-histories` to the git merge command. – Drasill Jan 20 '17 at 12:26
  • 3
    Info on comment from @Drasill: https://github.com/git/git/blob/master/Documentation/RelNotes/2.9.0.txt#L58-L68 – GaTechThomas Feb 22 '17 at 18:07
  • 17
    I have no idea what I'm doing and I can't really read this with foo/bar placeholders. Can anybody edit this with actual real life examples (like links where appropriate and such) ? – rien333 Aug 24 '17 at 14:48
  • Ooh, this is great. I'd prefer to merge it into a subdirectory. It can't be that different, is it? I'd prefer that step added. – macetw Oct 05 '17 at 20:30
  • 1
    I get tons of merge conflicts. Anyway you could force merge into a new branch? – nomadoda Aug 30 '18 at 06:29
  • If you want to create a separate branch without merge conflicts: `git checkout --orphan baz`, then `git rm -rf .` before merging – nomadoda Aug 30 '18 at 07:04
  • 1
    You saved my day! I was missing the `git remote update` command. – Tariq M Nasim Nov 21 '19 at 20:22
  • I think this is what I need. So repository foo exists and I copy it to newly created bar repository in my Git. At that moment foo == bar. Then I modify my bar repository while other modify the foo repository. Then when I want the updates from foo, I run the above commands - correct? And very important, it will prompt me on conflicts? FYI - the reason I need this is foo is open source and I'm making changes in bar that the open source team legitimately does not want. – David Thielen Apr 26 '22 at 18:13
92

Updated with "real-life" commands:

Start from your repo directory, make sure your working copy is clean (no files changed, added or removed).


Make a new branch:

git checkout -b <my-branch>

Add the secondary remote, then fetch it:

git remote add <repo-name> git@github.com:xxx/<repo-name>.git
git remote update

Merge one of their branches in your current branch:

git merge <repo-name>/<their-branch>


If you don't know which <their-branch> you want, then go for master

If you are sure you want to accept all remote changes and avoid conflicts (overwrite yours) then you can specify -X theirs as option for git merge in the last step.

If you want to add it in a subdirectory then you should probably use git submodules

vinzdef
  • 1,457
  • 1
  • 11
  • 21
  • 3
    Great answer, in conjunction with the accepted answer...helped me out with a big merge, which went relatively smoothly with the above command sequence. Thanks. – Jeff Wright Sep 27 '20 at 19:52
  • Another suggestion for "real life": Use an IDE that assists with git merges. For instance, VS Code's Git Lens lets you compare conflicts side by side and choose new, old, or both. Simplifies merge conflicts greatly. :) – Mike Williamson Mar 09 '21 at 09:27
  • 3
    if you get the git error "refusing to merge unrelated histories", then you can use the option `--allow-unrelated-histories` (details here: https://stackoverflow.com/a/37938036/1015581). – Jordy Jul 28 '21 at 19:25
6

Using the guide from larsks, I was able to do this using SourceTree.

  1. Created a branch in the destination repository
  2. Added the source repository as a remote, by hitting the Settings button and adding the source repository.
  3. Branches from both repository now show in the branch list. I used the merge tool to merge a branch from the source repository to my new destination repository's branch.
  4. Resolved any conflicts using either SourceTree or my IDE
  5. Commit the changes in my branch.
  6. Remove the source repository from the remote list, by using the Settings button.
AL - Divine
  • 945
  • 6
  • 8