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.
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.
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)
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
Using the guide from larsks, I was able to do this using SourceTree.
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 git@github.com
// 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.