Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fork from a branch in github

Is there a way to fork from a specific branch on GitHub? … For example, moodle has many branches (1.9, 2.0 … and so on). Can a clone be performed of just branch 1.9 and not the master branch always? Is it possible to clone a specific branch onto my PC?

like image 853
jan Avatar asked Feb 10 '12 12:02

jan


People also ask

Can you fork from a branch?

Previously, when creating a fork all branches from the parent repository were copied to the new fork repository. There are several scenarios where this is unneeded, such as contributing to open-source projects.

How do you pull from a fork branch?

Navigate to the original repository where you created your fork. Above the list of files, click Pull request. On the Compare page, click compare across forks. In the "base branch" drop-down menu, select the branch of the upstream repository you'd like to merge changes into.

How do you fork a single branch?

You can pull his branch into your local git repo, and then push it up to your GitHub hosted repo. Then make a local checkout of that branch in your repo. Finally, push that branch up to your repo hosted on GitHub. Save this answer.

How do I fork a branch on GitHub desktop?

Open the Repository menu, then click Repository settings.... Click Fork behavior, then select how you want to use the fork. Click Save.


1 Answers

I don’t know a native way yet, but you can do it following this recipe:

  1. Fork the repository in question (called ‘upstream’) on the GitHub website to your workspace there.
  2. Run the GitHub desktop application and clone the repository onto your PC.
  3. Use the GitHub desktop application to open a shell in the repository. (The git commands are not available from the default PowerShell unless you configure that manually.)
  4. Set the source repository as upstream:

    git remote add upstream https://github.com/{user}/{source-repo}.git 
  5. Fetch the full upstream repository. (Right now, you only have a copy of its master branch.)

    git fetch upstream 
  6. Make your file system copy the branch you want and give it any name:

    git checkout upstream/{branch-in-question} git checkout -b temporary 
  7. Publish your repo using the GitHub desktop application.

  8. On the GitHub website, open your repository and click ‘settings’.
  9. Change the “Default branch” to ‘temporary’. (Just change the drop-down menu, you don’t need to click the “Rename” button.)
  10. Go back to your repository, go to the ‘branches’ tab, now you can delete the “master” branch.
  11. Delete the master branch on your shell and make a new master branch:

    git branch -d master git branch master git checkout master git -d temporary 
  12. Once more, publish your repo using the GitHub desktop application.

  13. On the GitHub website, open your repository and click ‘settings’.
  14. Change the “Default branch” back to the (new) ‘master’ branch.
  15. Go back to your repository, go to the ‘branches’ tab, now you can delete the “temporary” branch.

This should be what you were looking for. Perhaps GitHub will provide a more convenient way to do this in future (e.g., clicking “Fork” from a project’s branch results in exactly this behaviour).

like image 51
Matthias Ronge Avatar answered Sep 21 '22 10:09

Matthias Ronge