60

I understand that forking is cloning the repository on the server side. But I don't understand why I would do that.

Why not clone the original repository to my machine, add my code, than push the new branch to GitHub and make a pull request?

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • 29
    Because you don't have push permissions to someone's GitHub repo – Kos Jul 03 '15 at 14:59
  • You lost me at push the new branch to Github – TheGeorgeous Jul 03 '15 at 14:59
  • 1
    @Kos So how can I later 'merge' my forked repository into the original? – Aviv Cohn Jul 03 '15 at 15:03
  • 2
    Also some people aren't just adding new features but want to completely branch out into a new project – Mauricio Trajano Jul 03 '15 at 15:03
  • 1
    @AvivCohn by opening a pull request. Whether it should be merged with the original is entirely upto the author of the original – TheGeorgeous Jul 03 '15 at 15:05
  • @TheGeorgeous of course, but that's what I need to do to add to the original project, right? – Aviv Cohn Jul 03 '15 at 15:07
  • @AvivCohn please clarify what do you mean by "pushing the new branch to GitHub". If you intend to push a new branch in the original repository, chances are you don't have the permission to do that because it is not your repository. So you clone in order to have a repository you have push permission, from which the original author can pull – pqnet Jul 03 '15 at 15:19

4 Answers4

92

I understand that forking is cloning the repository on the server side [...]

That's about right. On GitHub, a fork is a copy of some other GitHub repo, with a reference to the repo it was copied from.

enter image description here

Remark: the concept of a fork originated with GitHub; it is not a Git concept.

[...] but I don't understand why I would do that. Why not clone the original repository to my machine, add my code, then push the new branch to GitHub and make a pull request?

Unless you have write access to the repository in question, you cannot simply push anything to it; your push will be denied by the server with an error message of the form

remote: Permission to bos/text.git denied to Jubobs.
fatal: unable to access 'https://github.com/bos/text/': The requested URL returned error: 403

That's where a fork comes into play. By forking someone else's repo, you get a copy to which you have write access, i.e. to which you can push your contributions. The entire workflow goes something like this:

  1. Identify an area in Alice's repo that can be improved.
  2. Fork that repo.
  3. Make a clone of your fork on your local machine.
  4. In that clone, make changes, run tests, create commits (possibly on a new branch), etc.
  5. Optionally, once you're happy with your amendments to Alice's code, make your work more presentable: tidy/squash your commits, write good commit messages that respect the style of Alice's repo. You may also want to rebase your branch to that of Alice's branch, if Alice has pushed changes to her repo since you forked her repo.
  6. Push to your fork.
  7. Issue a pull request to Alice (essentially notifying Alice to take a look at the changes you made in your fork of her repo) and wait for her to review it.
  8. Push more commits to (and possibly rebase) your branch until Alice is satisfied with your work.
  9. If all goes well, Alice merges your pull request; your work gets integrated into her GitHub repo. Champagne! Your work was not in vain.
  10. If you don't intend to contribute to Alice's repo any time soon, you can safely delete your fork, to save space on GitHub's servers. If, on the other hand, you frequently contribute to Alice's repo, keep your fork and frequently sync it with Alice's repo.
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 1
    I would add 11. On other hand, if you frequently contribute to Alice's repo, sync your fork frequently with Alice's repo. – dzieciou Jul 07 '22 at 07:00
43

From the Github documentation

A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

Most commonly, forks are used to either propose changes to someone else's project or to use someone else's project as a starting point for your own idea.

https://help.github.com/articles/fork-a-repo/

TheGeorgeous
  • 3,927
  • 2
  • 20
  • 33
  • 4
    So why not `clone` the repository, implement my new feature as a new branch, push the branch (can you say that?) and issue a pull request on it? – Aviv Cohn Jul 03 '15 at 15:05
  • 16
    Normally you cannot push the new branch unless you have been added as a collaborator on that repo. Forking and creating pull requests is polite way of proposing changes. It is upto the author of the original to add the changes or not – TheGeorgeous Jul 03 '15 at 15:07
  • 1
    Do I have to fork in order to be able to make pull requests on a repository? – Aviv Cohn Jul 04 '15 at 16:27
  • 1
    yes and no. Depends on whether you have write access to the repo – TheGeorgeous Jul 04 '15 at 16:29
  • Forked repositories often cannot be used in PRs because for example travis doesn't support decrypting secrets in this case. – user239558 Mar 27 '17 at 08:02
  • Yea I understand that forking allows us to work independently. But if am not wrong, we can achieve the same by creating new branch on the same repo. Then what's the difference between forking a repo and creating feature branches ? – Krupa May 28 '21 at 09:57
  • 1
    @Krupa that is assuming you have access to create branches on some repo. Most open source communities won't give access to repos to other developers willy-nilly. – TheGeorgeous May 28 '21 at 10:13
  • @AvivCohn - lets presume YOU have a repository, and allow me to push to a branch (and only to that branch)..... Now, I turn out to be a "Bad actor" and push really horrible stuff, the kind that can get YOU in legal hot water.... In theory you could delete the branch, but there may be dangling objects for a long time... Also any backups taken (perhaps by GitHub or other provider) of YOUR repository will contain copies of that BAD material...... Practically speaking you cannot erase it to the point of preventing authorities from finding it... – David V. Corbin Feb 05 '22 at 12:49
  • On the other hand, if I fork the repository, then anything I do is on ME. You have NO involvement. At some point you look at my repository - and if I have been a bad actor, run away quickly - with your repository NEVER having been touched and you having no risk (technical, legal, moral) – David V. Corbin Feb 05 '22 at 12:49
6

Forking makes it clear that your repository derives from the other, by marking your repository as "forked from..." and more importantly, it will list your repository in the fork list of the original project.

One of the advantages of that is that in the event that the original project becomes discontinued while you are still working on it, people can still find your repository looking on the fork list from the main repository.

From github's point of view, they can save some disk space by knowing that the git objects between your and the original repository are the same and can be shared

Moreover, I think you cannot make a pull request from a repository which is not a fork of the original one. At least I wasn't able to do it, I had to fork, push to the fork, ask for a pull request.

pqnet
  • 6,070
  • 1
  • 30
  • 51
  • you can create pull requests between branches – TheGeorgeous Jul 03 '15 at 15:08
  • @TheGeorgeous I think the OP intends to push into a new repository in his own GitHub account, and you can't make pull requests between two different repositories if they are not fork-related – pqnet Jul 03 '15 at 15:10
  • @TheGeorgeous he did not specify _where_ he wants to push the branch. It may be another branch in the same repository, to which you may not have permission, or another repository to which he has write permission, from which he may not be able to issue a pull request. I think it is pretty rude to assume you guessed right and I guessed wrong. – pqnet Jul 03 '15 at 15:27
  • Whoa, whoa, no need for a flame war, forget I said anything – TheGeorgeous Jul 03 '15 at 15:34
  • Do I have to fork in order to make a pull request? If so, I understand why I'd want to fork. But I think you can issue a pull request simply by cloning, editing, pushing to the server and issuing the request. Am I wrong? – Aviv Cohn Jul 03 '15 at 21:49
  • @AvivCohn you can issue pull request either from a branch in the same repository (if you have write permits on there) or another repository which is a branch of the original one – pqnet Jul 06 '15 at 18:39
2

Another perspective - why fork within a company where you even might have push permission.

I prefer the forking workflow also withing my company. This keeps the upstream repo in a clean condition. It has only official branches and tags and it's not cluttered by 100+ or 1000+ of dev branches etc.. When you fetch the upstream you can clearly see what happened where as without forking, when fetching you get so many changes that it's too cluttered. There are teams that treat their repos as if they were external open source repos - i.e. they don't allow pushing anything to their repos - only pull requests from forks are allowed.

  • Nice addition! Yeah, it’s nice to be able to have a fork with just your own branches and tags which you can keep as messy or tidy as you want without interfering with the refs of anyone else. – Guildenstern Apr 28 '23 at 13:01
  • that's an excellent answer. Exact same problem is happening at my org, some of the apps have 2000+ branches. – Abhishek Tyagi May 04 '23 at 05:29