You can merge repository A into a subdirectory of a project B using the subtree merge strategy. This is described in Subtree Merging and You by Markus Prinz.
Use git to merge master into the branch. View a directory listing to validate files from master have been moved to the feature branch. View the Git reflog to validate that a master merge commit is registered. Push the merged feature branch back to the GitLab server.
First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch.
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> [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 probably you should probably use git submodules
Using the guide from larsks, I was able to do this using SourceTree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With