Search code examples
gitgithubgit-branchgit-remoterepo

How to clone ('fork') your own personal GitHub repo to a new repo in a separate, new branch?


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:

  1. I have a template repo username/template-repo
  2. Create an empty repo (manually or via git clone)
  3. Create a new, empty branch scaffold (so we have main & scaffold)
  4. Do a git remote add, so that the scaffold branch contains exactly the contents of the template-repo
  5. Merge or rebase the local scaffold into the main branch
  6. Work in the main branch and make commits.

If I want to get the new contents of the template-repo, I'd do sth like this:

  1. Git pull template-repo into scaffold branch
  2. Merge or rebase the scaffold branch with the main branch

I 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!


Solution

  • 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:

    1. 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
      
    2. 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
      
    3. Track the main branch on origin (instead of template):

      git push -u origin main
      
    4. 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
      
    5. 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]:

    1. Fetch the template repository to have the latest changes:

      git fetch template
      
    2. 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]:

    1. 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
      
    2. Merge scaffold into the desired branch. This could be main or another branch where you first integrate and test the changes (recommended).