GitHub - Pull changes from a template repository

Github

Github Problem Overview


I have created a Template Repository in GitHub and then created some repositories based on the template. Since they were created, there have been updates to the template that I want to pull into those repositories.

Is this possible?

Github Solutions


Solution 1 - Github

On the other repositories you have to add this template repository as a remote.

git remote add template [URL of the template repo]

Then run git fetch to update the changes

git fetch --all

Then is possible to merge another branch from the new remote to your current one.

git merge template/[branch to merge]

https://help.github.com/en/articles/adding-a-remote

Solution 2 - Github

I will link to the same location as HRK44 but my answer is very different.

https://help.github.com/en/articles/creating-a-repository-from-a-template

Although forks and templates are mentioned in the same section, they are very different.

One of the differences mentioned in the link is:

> A new fork includes the entire commit history of the parent repository, while a repository created from a template starts with a single commit.

This basicly means that you will not be able to pull new changes from the template as your git histories are very different and are not based on the same thing.

If you do use the method mentioned in the accepted answer, you will have very hard manual merges that will result in changes to all of the files received from the template, even if they werent changed since the first time you created that repo from that template.

In short, creating a repo from a template (using only master branch) is the same process as:

git clone template
cd folder
rm -rf .git
git init
git remote add origin <new repo url>
git add . 
git commit -m "Initial Commit"
git push -u origin master

A few other things that are not (surprisingly) copied when creating a repo from a template: (Unless github fix this at a later point)

  1. Repo configurations (allowed merge types, permissions etc)
  2. branch rules

So when using this at your organization, make sure to set all repo configurations on the newly created repo.

Solution 3 - Github

If you want to merge changes from a template into your project, you're going to need to fetch all of the missing commits from the template, and apply them to your own repo.

To do this, you're going to need to know the exact commit ID that you templated from, and you're going to need to know the commit ID of your first commit.

ORIGINAL_COMMIT_ID=<commit id from original repo you templated from>
YOUR_FIRST_COMMIT=<first commit id in your repo>
YOUR_BRANCH=master

Next you're going to need add the template as a remote, and fetch it.

git remote add upstream git@github.com:whatever/foo.git
git fetch upstream

And finally, you need to rebase all of the commits you're missing onto your branch

git rebase --onto ORIGINAL_COMMIT_ID YOUR_FIRST_COMMIT YOUR_BRANCH

What this is doing it basically creating a branch off of ORIGINAL_COMMIT_ID, then manually applying all of the commits on your original branch, onto this new branch.

This leaves you with what you would have had, if you had forked.

From here, you can git merge upstream/master just as if you had forked.

Once you've completed your merge, you'll need to use git push --force to push all of the changes up to the remote. If you're working with a team, you'll need to coordinate with everyone when doing this, as you're changing the history of the repo.

Note: It's important to note that this is only going to apply to one branch. If you have multiple feature branches, you'll need to perform the same steps to each one.

Solution 4 - Github

@daniel's answer also did not work for me because of the unrelated histories problem mentioned in @dima's answer. I achieved the desired functionality by doing the following:

  1. Copy the URL for the template repository you wish to use to create a new repository. (ex: https://github.com/<username>/my-template.git)

  2. Use GitHub Importer to make a new repository based on the template repository.

    This solves the unrelated histories problem because it preserves the entire commit history of the template repository.

    You need to use the Importer because you cannot fork your own repository. If you want to use someone else's template repository, you can just fork theirs.

  3. Then, add the template repository as a remote.

    git remote add template https://github.com/<username>/my-template.git
    
  4. After you make new commits to the template repository, you can fetch those changes.

    git fetch template
    
  5. Then, merge or rebase. I recommend to merge on public repos and rebase on private repos.

    To merge

    git checkout <branch-to-merge-to>
    git merge template/<branch-to-merge>
    

    To rebase

    git checkout <branch-to-merge-to>
    git rebase upstream/<branch-to-merge>
    

    > NOTE: When rebasing, you must > > git push origin <branch-name> --force > > in order to override your old commits on your remote branch. This is why I recommend to rebase only on private repos.

Solution 5 - Github

I approached this differently as fetch & merge was not ideal as lot of files diverge across template and downstream projects. I only needed the common elements to sync.

lets says we have the below folder structure locally:

repos
├── template_repo
└── downstream_repo

1. Now create a git patch from the parent folder (repos):

git diff --no-index --diff-filter=d --output=upstream_changes.patch -- downstream_repo/some_common_path template_repo/some_common_path

NOTE - the order of the paths matters!, downstream_repo comes first! (interpret this as "what are the changes we need to make to downstream_repo to make it same as template_repo"; look at the --name-status output, it will hopefully make sense.)

--no-index option generates git diff based on filesystem paths. Path can be a single file or a folder.

--diff-filter=d will ignore any files that are in the downstream_repo but not in the template_repo. This only applies when diffing folder paths.

You can use --stat, --name-status to see what the patch will contain. Review the generated patch.

2. Change to downstream_repo folder and apply the patch

git apply -p2 ../upstream_changes.patch

explanation for -p<n> option from the official doc:

> Remove leading path components (separated by slashes) from > traditional diff paths. E.g., with -p2, a patch against a/dir/file > will be applied directly to file. The default is 1.

Using -p2 will drop a/downstream_repo and b/template_repo from the diff paths allowing the patch to apply.

This is the reason for starting with above illustrated folder structure. Once the patch is applied, rest of the process should be familiar.

All of these options are clearly explained in git diff and git apply official docs.

Solution 6 - Github

Based on this link https://help.github.com/en/articles/creating-a-repository-from-a-template, creating from a template is equivalent to a fork (with some subtle differences).

So I guess you could add the template as a remote, then occasionally fetch/merge the changes into your current repo.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew LaytonView Question on Stackoverflow
Solution 1 - GithubDaniel DobladoView Answer on Stackoverflow
Solution 2 - GithubDima GoldinView Answer on Stackoverflow
Solution 3 - GithubCerealView Answer on Stackoverflow
Solution 4 - GithubjoshView Answer on Stackoverflow
Solution 5 - GithubyudiView Answer on Stackoverflow
Solution 6 - GithubHRK44View Answer on Stackoverflow