You find a typo in your favourite open source project and want to fix it. An open source project is a project whose code is open: anyone may read it and propose changes. But the repo isn't yours, so a git push to it will be refused. How can your fix reach the project, then?
Through a Pull Request (PR). With a PR, you propose a change from a branch. Other people read and judge that change (review), and then the repo's owner combines it into the repo (merge). It's like proposing a fix to the family recipe book that belongs to Grandma. You're not allowed to write in her book directly. So you photocopy the book first, write your fix on the copy, then bring it to Grandma: "I'd like to change this recipe this way." Grandma reads it, maybe asks for a small change, then copies it into the real book. On GitHub, Grandma's book is the original repo, and your photocopy is a fork (a copy of the repo in your GitHub account). Bringing the copy to Grandma is opening a PR, and copying the fix into the real book is the merge.
Three places for a repo: original, fork, and clone
Take the repo belajarweb/glossary, a glossary of web terms owned by the GitHub account belajarweb. You find a typo in it: it says Comit where it should say Commit. You don't have write access (permission to push) to that repo, so your change travels through three places:
| Place | Address | What it is |
|---|---|---|
| Original repo (upstream) | github.com/belajarweb/glossary |
Someone else's. Where your change should end up. |
| Fork | github.com/your-name/glossary |
A copy of the original in your GitHub account, made with the Fork button. You can push here freely. |
| Clone | the glossary folder on your laptop |
A copy of your fork on your computer, made with git clone. This is where you edit and commit. |
So the difference is: a fork is a copy on GitHub, a clone is a copy on your computer. To add your own changes to an open source project, the first step is to fork, then clone your fork.
From fork to Pull Request
Click the Fork button on the github.com/belajarweb/glossary page. Once your fork your-name/glossary exists, continue in the terminal (some lines of Git's answers are left out):
$ git clone https://github.com/your-name/glossary.git
Cloning into 'glossary'...
$ cd glossary
$ git switch -c fix-typo
Switched to a new branch 'fix-typo'
$ git add glossary.md # after changing Comit to Commit in the editor
$ git commit -m "docs: fix the Comit typo in the glossary"
[fix-typo 3f2a91c] docs: fix the Comit typo in the glossary
1 file changed, 1 insertion(+), 1 deletion(-)
$ git push -u origin fix-typo
remote: Create a pull request for 'fix-typo' on GitHub by visiting:
remote: https://github.com/your-name/glossary/pull/new/fix-typo
To https://github.com/your-name/glossary.git
* [new branch] fix-typo -> fix-typo
git clonecopies your fork to the laptop, into a new folderglossary, andcd glossarygoes into that folder. In this clone,originpoints at your fork, not at the original repo.git switch -c fix-typo(the same asgit checkout -b fix-typo) makes a new branch and moves onto it. The rule: one change, one branch.git addandgit commitrun as usual. Your commit hash will differ from this example.git push -u origin fix-typosends that branch to your fork on GitHub.-urecords thefix-typobranch on your fork as this branch's fixed partner, so the next push is justgit push.- GitHub right away gives you a link to open a PR, in the lines that start with
remote:.
Open that link (or click the Compare & pull request button on GitHub), then pick the direction. The base (the target) is belajarweb/glossary branch main, and the compare (where the change comes from) is your-name/glossary branch fix-typo. Fill in a title and a description, then click Create pull request. Now there's PR #12, which says your-name wants to merge 1 commit into belajarweb:main from your-name:fix-typo. It means you want to merge 1 commit from your fix-typo branch into the main of belajarweb.
So a Pull Request is the GitHub feature for asking to merge code from one branch into the main branch, so it gets reviewed before it's merged.
Review, then merge
The repo's owner, for example rina from belajarweb, reads your change in the Files changed tab. She has three choices:
- Comment: only leave a remark.
- Approve: agree, the change can be merged.
- Request changes: ask for fixes first.
For example, Rina asks: "Please add an example command too." You don't need a new PR. Edit the file on your laptop, commit, then push to the same branch:
$ git add glossary.md
$ git commit -m "docs: add a git commit example"
[fix-typo 8d4e7b2] docs: add a git commit example
1 file changed, 1 insertion(+)
$ git push
To https://github.com/your-name/glossary.git
3f2a91c..8d4e7b2 fix-typo -> fix-typo
PR #12 is updated too and now holds 2 commits. The reason: a PR points at a branch, not at one particular commit, so new commits on that branch become part of the PR. After Rina clicks Approve, she clicks Merge pull request. A merge commit Merge pull request #12 from your-name/fix-typo appears on the original repo's main, and the PR's status becomes Merged.
GitHub's merge button has three options (the repo owner can turn some of them off):
| Option | Result on main |
Good for |
|---|---|---|
| Create a merge commit (the default, used by Rina) | all the PR's commits, plus one merge commit | a full history of every commit |
| Squash and merge | all the PR's commits packed into one new commit | a PR with many small commits such as "fix typo again" |
| Rebase and merge | the PR's commits copied one by one onto the end of main, with no merge commit |
a straight history |
After Squash and merge or Rebase and merge, your original commits don't go into main: copies of them do. So once the branch is deleted on GitHub and git fetch --prune removes origin/fix-typo, git branch -d fix-typo on your laptop can refuse with error: the branch 'fix-typo' is not fully merged. With no upstream, -d checks the branch you're on, for example main. If the PR really was merged, delete the branch with git branch -D fix-typo.
💡 If you're on the team
- Have write access? No fork needed. Clone the team repo directly, make a branch, push to the team repo, then open a PR from that branch into
main. - Branch protection. The repo's owner can lock
mainwith rules: no pushing to it directly, a PR needs at least 1 approval, and automatic checks (GitHub Actions) must pass. Until there's an approval, the merge button stays locked with the messageMerging is blocked. - Issues are where you record a bug or an idea before there's any code for it. Write the steps that make the bug appear. The PR that fixes it can mention the issue's number, for example
Fixes #7, so that issue #7 closes automatically when the PR is merged. This automatic closing works only for a PR into the repo's main branch, usuallymain.
⚠️ So your PR is easy to review
- Committing straight onto your fork's
main. Because a PR points at a branch, all your changes onmainget mixed into one PR. Make a new branch for each change. - Opening a new PR for every revision. For a revision (a fix after a review), just push to the same branch. The PR updates itself, and the discussion stays in one place.
- A fork that is out of date. The original repo keeps getting new commits, but your fork doesn't get them. Use the Sync fork button on GitHub. You can also get them in the terminal:
git remote add upstream https://github.com/belajarweb/glossary.gitadds the original repo as a remote namedupstream, thengit pull upstream maintakes the new commits from the original repo'smaininto your clone. - A giant PR. A PR that changes 50 files at once is hard to review. A small PR, with a clear title and a description of "what changed and why", gets approved faster.
TL;DR: For someone else's repo that you can't push to: Fork it (make a copy in your GitHub account), git clone the fork (a copy on your computer), git switch -c a new branch, commit, git push -u origin <branch>, then open a Pull Request (PR) into the original repo's main. A PR proposes merging your branch, and it is reviewed (read and judged) before it's merged. A reviewer can Comment (leave a remark), Approve (agree), or Request changes (ask for fixes). Revisions (fixes after a review) are simply pushed to the same branch, and the PR updates along with it. Once approved, the PR is merged, and the merge commit Merge pull request #12 from ... appears on the original repo's main. Team members with write access (permission to push) don't need a fork.
⚡ Fork first, then clone
Guess first: a fork and a clone both copy a repo. What's the difference?
A fork is a copy on your GitHub account, a clone is a copy on your computer. For someone else's repo, the first step is a fork, then you clone your fork.
Meaning: You don't have write access (permission to push) to someone else's original repo, so your changes are pushed to your own fork.
⚡ One change, one branch
Write:
git clone https://github.com/your-name/glossary.git
cd glossary
git switch -c fix-typo
git add glossary.md
git commit -m "docs: fix the Comit typo in the glossary"
git push -u origin fix-typo
Meaning: Clone your fork, go into its folder with cd glossary, make a branch just for this change, commit, then push. In this clone, origin points to your fork, not the original repo. Once the branch is pushed, GitHub gives you a link to open a PR (a Pull Request: a proposal to merge your branch into the original repo).
⚡ Open a Pull Request
Meaning: A Pull Request proposes merging your branch into the original repo's main, so it gets reviewed (read and judged) first. A reviewer can choose Comment (leave a remark), Approve (agree), or Request changes (ask for fixes). Once it's approved, the repo's owner clicks Merge pull request.
⚡ Revisions? Push to the same branch
Meaning: If the reviewer asks for fixes, no new PR is needed. Commit the fix on the same branch, then git push. The PR is updated too, because a PR points to a branch, not to a particular commit.