Fork from a branch in github

2019-03-08 03:31发布

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?

7条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-08 04:09

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).

查看更多
干净又极端
3楼-- · 2019-03-08 04:10

Switch to the branch you need in source repo Click "Fork". You'll get forked master and the branch you're in. I don't know how it works with more branches, but for my needs worked pretty well.

查看更多
Explosion°爆炸
4楼-- · 2019-03-08 04:12

Cloning means that you create a copy of the whole repository in your account including all branches and tags. However you are free to switch and track branches however you like.

查看更多
放我归山
5楼-- · 2019-03-08 04:12

A fast, alternative approach is to create your own new repo.

Go to https://github.com/new and make a new repo. Do not initialize with README.

Scroll down to get your git remote

enter image description here

Then:

git remote rm origin
git config master.remote origin
git config master.merge refs/heads/master
// Run code from above image
git push --set-upstream origin yourbranchname

You will have a new repo with the original repo's code and a branch that can be made into a pull request.

查看更多
仙女界的扛把子
6楼-- · 2019-03-08 04:32

I'm using bitbucket but I'm sure this would work for GitHub as well.

  1. Create a new repository
  2. Checkout the branch using GitExtensions
  3. Click Push to open the Push dialog
  4. Set the destination URL to the new repository
  5. Set the destination branch to "master"
  6. Push

Your new repository will have the full history of the one branch only (not all branches like forking will have).

GitExtensions Push Dialog

查看更多
虎瘦雄心在
7楼-- · 2019-03-08 04:33

Yes, you can clone the single branch. For example, you have a branch named release1.0. If you would like to clone this branch into your pc then use the following line of code:

$ git clone git@bitbucket.org:git_username/git_repository_example -b release1.0 --single-branch
查看更多
登录 后发表回答