224

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.

1
  • If you have homebrew hub command installed, you can do hub merge <github-pr-url-from-the-browser-omg>. But it fails until you do git remote add upstream <the repo url>.git before hand. I don't know if remote name needs to be upstream. Commented May 24, 2021 at 22:43

6 Answers 6

390

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 [email protected]: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)

Sign up to request clarification or add additional context in comments.

6 Comments

since git 2.9 you'll probably need to add --allow-unrelated-histories to the git merge command.
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) ?
I get tons of merge conflicts. Anyway you could force merge into a new branch?
If you want to create a separate branch without merge conflicts: git checkout --orphan baz, then git rm -rf . before merging
|
127

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> [email protected]: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

3 Comments

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.
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. :)
if you get the git error "refusing to merge unrelated histories", then you can use the option --allow-unrelated-histories (details here: stackoverflow.com/a/37938036/1015581).
7

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.

Comments

5

The popular rien333 comment on the accepted answer asked for the commands with filled-in examples.

For example, I'm using the LLM chat boilerplate Big-AGI and have my own Github repo which is not a fork but is a copy. A few weeks later, I want to integrate the latest changes.

First thing is I need to make sure I have a SSH key setup on GitHub.

Verify SSH key is properly set up with

ssh -T [email protected]

// Hi andrewschreiber! You've successfully authenticated, but GitHub does not provide shell access.

Once that is done, I navigate to my repo in terminal and enter

git checkout -b update-from-boilerplate1
git remote add big-agi https://github.com/enricoros/big-agi
git remote update
git merge --allow-unrelated-histories big-agi/main
// Solve any merge conflicts
git checkout main
git merge update-from-boilerplate1

You can always get the latest updates by running remote update.

Comments

2

Can be done ad-hoc, without having to add the other repo as a remote:

git fetch [email protected]:mountypython/deadparrot.git remote-parrot-branch:local-parrot-branch-tmp

git merge local-branch-tmp

If histories are unrelated then add --allow-unrelated-histories, assuming that you know what you are doing.

Comments

0

I usually do the following for merging master branches of two repositories into the one:

git remote add origin2 ../relative-or-absolute-path-to-other-repo
git fetch origin2 master
git merge origin2/master --allow-unrelated-histories
git remote remove origin2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.