difference between fork and branch on github

GithubBranchGit BranchGit Fork

Github Problem Overview


If I fork a project that's hosted on github. Do I fork all the branches? How do I know which branch my fork is based on? In other words which branch will be downloaded to my PC?

Github Solutions


Solution 1 - Github

Think of it this way:

The repo[sitory] corresponds to the collaborated work of the team across one or many branches. All contributors have their own copy of it.

Each fork of the main repo corresponds to a contributor's work. A fork is really a Github (not Git) construct to store a clone of the repo in your user account. As a clone, it will contain all the branches in the main repo at the time you made the fork.

Each branch within the fork and/or in the main repo can correspond to several kinds of things, depending on how you want to work. Each branch could refer to a version of the project but can also correspond to different channels of development, like hotfixes or experimental work.

The pull request (in the GitHub ecosystem) corresponds to the task. Every time I want to contribute an isolated finished task to the main repo, I create a pull request corresponding to the commits made in that task. These commits are pulled from either my fork or my branch to the main repo.

A commit is a set of changes to the code. This is one of the most interesting things about Git. You don't transfer files, you transfer logs of changes.

Solution 2 - Github

All branches on GitHub will be copied in a fork. (Obviously, this doesn’t include branches that were never pushed to GitHub in the first place.)

But a fork is a GitHub-to-GitHub operation; nothing is copied to your PC. It’s not quite the same as a Git clone. If you mean to ask “what’s copied when I clone a project?”, see the manual for git-clone(1).

Solution 3 - Github

Fork is a clone on the GitHub side (it clones everything).
When you are cloning a repo, you are getting the all history of said repo, with all its branches.

Even though you can in theory change the default branch of a remote repo, a clone from a GitHub repo mainly look for the master branch. Meaning to change the "default" branch a GitHub clone will get, you need to rename the master branch.

Solution 4 - Github

If you fork a project, you are making a copy of the whole project to your git hub account. you are not coping anything to your PC

To make a copy in your PC you have to clone it and pull all the stuff and you will got all branches & code of that project

Solution 5 - Github

If you create a fork of a project from the Github website, you get all the branches from the upstream project.

If you clone from your newly minted fork to your local PC, you will have the origin remote on your PC pointing to the master branch of your fork on Github.

Solution 6 - Github

This can be explained very well. You have a central repository at GitHub. Whenever you take a clone of it on your personal computer to do some changes, this local clone of the main repository is called a fork.

The branch is something different and is included in the fork/repo. Actually the branch is your work at different stage of development. They are created as and when required to save a set of functionalities, to give access to different users, to demonstrate the site to client, etc.

Solution 7 - Github

I would like to share a real life example of when we use Branches and when we use Forks

We have GitLab at our shop and sometimes we have to work on packages from a Laravel project. We normally create a branch and push changes to the branch that we have been testing in our local VM dev environment when working with the actual Laravel project.

Let's say our project is located at

https://github.com/yardpenalty/mainproject.git

Branch usage:

Lets say the branch is called It_doesnt_matter

Once we have our branch the way we want for production we then make our final push to this branch and create a merge request which then goes into UAT for testing.Once the test has went through QC the changes are merged into production.

The merge from the It_doesnt_matter branch is now pushed to the master project

at https://github.com/yardpenalty/mainproject.git

Let's say the package project is located at

https://github.com/yardpenalty/mypackage.git

Keep in mind the mainproject uses this package in production so we can't make changes by simply pushing them to this package (among other reasons). Let's say a web dev has to edit this package to make changes on production.

A simple branch wont work either because we can't see our changes without publishing the package etc.

Fork Usage: Now is when we have to do a little bit of trickery with our package so we create a clone of the production package via a fork. The composer.json files can be updated to point to the fork which is now located at a User or Group path

So we will create a fork in https://github.com/yardpenalty/mypackage.git

and call it https://github.com/yardpenalty/yards/mypackage.git

Now we can update our composer.json file to point to this package in our "repositories":[ array like such such and away we go!

 {
            "type": "github",
            "url": "https://github.com/yardpenalty/yard/mypackage.git"
 }

]

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
QuestionJonathan.View Question on Stackoverflow
Solution 1 - GithubAdam GrantView Answer on Stackoverflow
Solution 2 - GithubJ. C. SalomonView Answer on Stackoverflow
Solution 3 - GithubVonCView Answer on Stackoverflow
Solution 4 - GithubHuntView Answer on Stackoverflow
Solution 5 - GithubbhambyView Answer on Stackoverflow
Solution 6 - GithubAvinash ThombreView Answer on Stackoverflow
Solution 7 - Githubyardpenalty.comView Answer on Stackoverflow