Problem with GitHub template repository when I include all branches

I have a template repository on GitHub with 3 branches: master, develop and feature/auth and the reason for that is because the code of the branch feature/auth is optional depending on if the web application has authentication or not.

The problem I have is when I choose the option “Use this template” to generate new repositories and include all branches, then clone the new repository and merge feature/auth with develop an error shows up:

fatal: refusing to merge unrelated histories

I tried running git merge feature/auth --allow-unrelated-histories

But I got a lot of conflicts and I don’t want people who use the template to have to solve conflicts.

I don’t have this problem with the original repository, only with the template generated ones.

This are the steps to reproduce the issue:

  1. navigate to the template repository.
  2. click the option “Use this template”.
  3. fill the repository name field with any name you want and check the “Include all branches” checkbox, then click the “Create repository from template” button.
  4. after the new repository is generated, clone it to your computer.
  5. Enter the repository directory and run the following commands:
$ git fetch --all
$ git checkout feature/auth
$ git checkout develop
$ git merge feature/auth

I’ve asked in StackOverflow and some other forums but got no help at all. Can somebody please help me with this?

Thank you very much in advance.

1 Like

Hi @juanmesa2097 :wave:

Thanks for coming into the community!
In order to save your future self and any other user that chooses to clone this repository the pain of fixing those conflicts, I recommend addressing those conflicts yourself and then push to your repository.

Hello @ernest-phillips, thanks for answering.

When you’re in the original repo no conflicts occur but when you use the original repo as a template and generate a new repository, then you got a lot of conflicts when you merge feature branches into develop branch.

1 Like

Hi @juanmesa2097 ,

In this case , a template repository may not be the best path here. We suggest using forks as they include the entire commit history of the parent repository, while a repository created from a template starts with a single commit.

Since a template leaves you with a single commit and no shared history, merges will be problematic.

I am having the same issue;
I am creating a repo on GitHub, just readme, git ignore and license.
From main I create a new branch, development;
From development I create a new branch, feature;
Make changes to feature, add commit, push to github;
Only feature have new code.
Turn into a template via settings.

I create a new repo and want to use as template including all branches.
Clone the repo, dot he job.
Add commit push.
Unrelated histories fatal error.
have all the branches but i can’t merge feature to development.
I was expecting a different flow;
With a template you want to replicate project including files and structures, branching including;
And in the newborn projects to operate as always.
add commit push merge
How to fix this in a clean way?
I mean, without solving conflicts not related to our work, and without using flag like --allow-unrelated-histories?

2 Likes

I also have this same issue. When I create a repo as a template and try to create a new repo from this template and check the box that says to include all branches, my new repo says ‘develop had recent pushes less than a minute ago.

When I try to Compare and pull request into master I am met with " There isn’t anything to compare. master and develop are entirely different commit histories."

1 Like

This is exactly the kind of information that should be in the description of what template repositories are. Cloning a template has odd behavior and I can’t find it documented anywhere. However, I don’t have any options in using GitHub Classroom other than templates.

Even if all commits are ‘squashed’ on forking a template, it should not be acceptable to disconnect branches in the template. Either:

  • remove the branches and warn the user who is creating the template that the branches are gone
  • recreate all branches at the same commit as the default branch
1 Like

Forking the repository is not an option either. I can’t fork to my own account. GitHub template repositories are the perfect solution but unfortunately, there is this downside of conflicts. I hope this gets fixed if it’s possible.

I’m having the same problem here. We should be able to A, merging branches freely on the newly created repo or fork to the same person/organization to solve these issues.

Just had the same problem and google led me to this thread. It seems like being able to get all the branches from a github template is pointless if you can not merge them like a regular repo.

I propose a feature request(s):
-Have the option (or default) to get the full comit history when cloning the template so merges can actually work
OR
-Allow the user to pick one and only one branch since the other branches can not be merged into

Regardless of either, this should be in the docs (like a warning when you do decided to get all the branches)

Thanks.

I’ve had many problems with template repositories with additional branches as well. I’ve found a very hacky way to get around the unrelated history issue:

git checkout branch-with-changes
git rebase origin/master -Xtheirs
git push -f origin branch-with-changes

That said, I agree with the general sentiment that commit histories should retain their integrity and allow merging (Even if each branch is squashed to a single commit).

Hi everyone :wave:

I know that this issue has been a blocker for many of you, and I wanted you to know that we are working on this as a team to get it fixed.

Our recommended workaround is to use --allow-unrelated-histories when you run into the merge conflict error.

For those that use this approach, but still run into problems, please drop a link to your template repository in the thread. We hope to use this information to develop some kind of pattern as to why this may be happening.

What error are you getting after attempting the merge?

@juanmesa2097 @scottdaniel @IlanVivanco @adwolfe-loyno @jvschoen

Thank you all for your input.

Hi @ernest-phillips :wave:

First of all, thanks for giving it a try! Great news!

As you said, I solved it using –allow-unrelated-histories. But it would be much better if you either solve it or at least document it for future users.

1 Like

Thanks for the feedback @IlanVivanco .

I can certainly sympathize with that position. We are working towards a permanent solution here. I think it would be a good idea to notate the current functionality somewhere as well in our documentation.

1 Like

Yeah, like @IlanVivanco I don’t get any error with --allow-unrelated-histories but it is a pain to go through every file and edit them before committing. Thanks for the attention you’re giving this @ernest-phillips

2 Likes

I’m toying with a fuzzy idea of using GitHub Actions to overcome this limitation until a more permanent, behind-the-scenes feature-fix is in place. I could see having a Workflow create any non-default branches based off of the default branches (in my case, ‘dev’, ‘qa’, etc.) so they have a shared history, and, using GitHub Script · Actions · GitHub Marketplace · GitHub (or similar method) to create Branch Protection rules (or any other GitHub UI elements associated with the template repo).

I feel like there must be prior-art here, tho? Plus, I don’t know how to create a ‘run-once’ GitHub Action that would only run when the repo is first created from the template. Any suggestions here are very welcome! :slight_smile:

Cheers.