Noop: best practice - master from other projekt

Hi,

how is the best practice if the master-projekt is not under my own control, but i would like merge with this.

This is what the project looks like, which is not under my control and i don’t can merge in the master or branches from this:

Master-Projekt
-> Branch-Current Version
/admin/ --File1 --File2 -File3
/inkl/–File1 --File2 -File3
/templ/ProjektTempl/

This is how the template project should looks:
-> Own-Template
/templ/OwnTempl/ --File1 --File2 -File3

Now I would like to merge the two (Current Version + Own Template) together.

-> New
/admin/ --File1 --File2 -File3
/inkl/ --File1 --File2 -File3
/templ/ProjektTempl/
/templ//OwnTempl/ --File1 --File2 -File3

Thanks!

Hi,

so you are having two Git repositories, and the master-repo should be included into your Git repository tree.

Git submodules or subtrees don’t really qualify for this, since they expect to live in a sub directory. So you’ll directly need to merge two repository histories together.

Merge two repositories

The following describes a one-shot merger. Important: This retrieves the entire history of the master project which may render your project’s history unreadable.

Local own repository

git clone https://gitlabserver/group/own-templates.git
cd own-templates

Add secondary remote repository config

git remote add master-project https://gitlabserver/othergroup/master-project.git
git fetch master-project

Now the following remotes exist:

  • origin as default for our repository
  • master-project pointing to the other repository where we want to import data from.

The git fetch updated the local index for changes, but did not pull any commit history yet.

Branches and Merging

Using the detail that you can access <remote>/<branch> of any configured and fetched remote, you can immediately access master-project/master as a reference and merge source.

git checkout master
git merge --allow-unrelated-histories master-project/master

Future Updates

Same as above but without the remote configuration step.

git fetch master-project

git checkout master
git merge --allow-unrelated-histories master-project/master`

Conclusion

If the above is too complicated and you’d just want to incorporate the changes from the upstream project once, there is no shame with just copying the source tree from one git repo to the other and committing it as “Vendor updates”.
We do that for third-party sources whenever Git subtrees won’t work well enough, or we’ll only need partial content of the repository, e.g. a minified JS file, and not the entire source tree.

For you to learn - get to know more about Git. There’s the online book available, and my employer has open sourced their GitLab training too.

Cheers,
Michael

Edit: Updated from feedback below.

Wow, thanks a lot!

Hi again.

Your description is great. Unfortunately, the merge is still not working. However, if I commit after the merge, I don’t have the files in the branch directory.

git checkout origin/template
ls -la 
drwxr-xr-x 3 user user 4096 Jan  9 14:34 templates
git checkout master-project/releasX
ls -la
drwxr-xr-x  8 user user  4096 Jan  9 14:49 admin
drwxr-xr-x  8 user user  4096 Jan  9 14:49 inkl
...
git checkout origin/template
git merge --strategy=ours --allow-unrelated-histories master-project/releasX

git checkout origin/template
ls -la 
drwxr-xr-x 3 user user 4096 Jan  9 15:00 templates

But by the checkout to origin/template I get a massage like:

Previous position of HEAD was 671ce4312 Merge branch 'merge commit from the master-projekt'
HEAD is now at 2f4370123 'my last commit before the template+master-projekt merge '

Do you have an idea?

Thanks again.

That workflow doesn’t make sense. The first command brings you into a detached head without any branch target.

You really need to change into your local master branch inside of the current project and then run the merge command.

If that’s not master, but template, also fine. But omit the origin/ prefix, this must not be used here.

Committing after a merge isn’t necessary, unless you made local changes after the merge.

Cheers,
Michael

Thanks again for your answer!

The “origin” was only that you see that I in the ride projekt.

If I do it without the commit I get this:

git checkout master
git merge --strategy=ours --no-commit --allow-unrelated-histories master-project/releasX
> Automatic merge completed; stop before committing as desired
ls -la 
> drwxr-xr-x 3 user user 4096 Jan  10 8:30 templates

If I merge some other after this from by template-projekt in the master I get this:

git checkout master
git merge someother
> fatal: you have not completed your merge (MERGE_HEAD exists).
> Please commit your changes before performing the merge.

If I do it with the commit I get this:

git checkout master
git merge --strategy=ours --allow-unrelated-histories master-project/releasX
> Merge made by the 'ours' strategy.
ls -la 
> drwxr-xr-x 3 user user 4096 Jan  10 8:05 templates 

But I never get the output like with a project internal merge.

git checkout master
git merge someother
> Update 7d4c19a..2f43703>
> Fast-forward
> testfile.txt | 6+
> create mode 100644 testfile.txt
ls -la
> drwxr-xr-x 3 user user 4096 Jan  10 8:05 templates 
> -rw-r--r-- 10 user user 4096 Jan 10 09:10 testfile.txt

Do you have any ideas?
Really thank you!

Hi,

aaaaaaah. Sorry, I overlooked the --no-commit parameter, that tells Git to halt and create your own merge commit. I normally don’t use that parameter, should drink more coffee when copying things.

Hm, the merge is weird. It might be the case that --strategy=ours prevents the combined merge. Please try to remove that parameter as well :slight_smile:

Cheers,
Michael

1 Like

Wow, that was it!

git clone https://gitlabserver/group/own-templates.git
cd own-templates
git remote add master-project https://gitlabserver/othergroup/master-project.git
git fetch master-project
git checkout origin/branch
git merge --allow-unrelated-histories master-project/branch
ls -la
drwxr-xr-x  8 user user  4096 Jan  13 10:00 admin
drwxr-xr-x  8 user user  4096 Jan  13 10:00 inkl
drwxr-xr-x  8 user user  4096 Jan  13 10:00 templ
cd templ
ls -la
drwxr-xr-x  8 user user  4096 Jan  13 10:01 ProjektTempl
drwxr-xr-x  8 user user  4096 Jan  13 10:01 OwnTempl

Thanks!

I’ve updated the original post, thanks :slight_smile: Glad that this now works.