I have a personal GitHub repo that I want to use as a starting point for new projects = other repos. This might be very easy for you, but I'm really a beginner, so I hope you can help me with this.
If I make changes to that scaffold repo, I need the option to merge/rebase them into the projects that stem from it. So a GitHub template repo will not do the trick.
I'm thinking of the following solution:
username/template-repo
git clone
)scaffold
(so we have main
& scaffold
)git remote add
, so that the scaffold
branch contains exactly the contents of the template-repo
scaffold
into the main
branchmain
branch and make commits.If I want to get the new contents of the template-repo
, I'd do sth like this:
template-repo
into scaffold
branchscaffold
branch with the main
branchI understand how Git works and I know what I need, but I just have difficulty setting this all up. I'd be very grateful if you could point out how I'd do this.
Thanks a lot!
As already noted in the comments, you are confusing terms. A repository/fork cannot be "compressed" into a single branch. However, I think your explanation is clear enough to give an answer to your question.
You can configure multiple remotes and this seems to be a good use case for this. It is somehow similar as setting an upstream
repository when forking another repository. Instead of upstream
, you basically have a template
repository. Much of the same concepts apply since the fork should get changes from the upstream/template repository.
The setup could be something along these line:
When cloning, you give the remote repository the name template instead of origin:
git clone -o template <repository> <directory>
# example: git clone -o template https://.../username/template-repo.git project-dir
Add the remote repository for the project itself and push all branches:
git remote add origin https://.../username/project-repo.git
git push origin --all
Track the main
branch on origin
(instead of template):
git push -u origin main
Set up a local branch that tracks a remote branch in the template repository (only needed when update-option B is used later):
git branch --track scaffold template/main
Optionally, overwrite the push URL of the template repository so you don't accidentally push there from your project repository:
git remote set-url --push template do-not-push
Updating the project repository with changes from the template repository [Option A]:
Fetch the template repository to have the latest changes:
git fetch template
Merge the changes of template/main
into the current branch*:
git merge template/master
*Note that the current branch could be directly your project's main
branch or another branch which will be used to test everything first (recommended) and then merge into main
later on – maybe with a pull request.
Updating the project repository with changes from the template repository [Option B]:
When you have set up a remote tracking-branch scaffold
(see step 4 in the setup), you can check it out and pull the changes:
git checkout scaffold
git pull
Merge scaffold
into the desired branch. This could be main
or another branch where you first integrate and test the changes (recommended).