GitHub Workflow — Git

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…

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

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:

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

⚠️ So your PR is easy to review

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.

What you will learn