TExcelTAD
Home › Git › GitHub Fork

GitHub Fork

To contribute to a project you don't have write access to (most open-source projects), you fork it: GitHub makes a copy of the repository under your account. You push to your fork, then ask the original project to pull your changes with a pull request.

   original (upstream)          your fork (origin)            your computer
  github.com/tealab/kettle -> github.com/ada/kettle  --clone-->  ~/kettle
          ^                                                         |
          +-------------- pull request <---- git push --------------+

Forking

On the project page, click Fork, choose your account, and GitHub creates github.com/<you>/<project>. A fork is a server-side git clone: same commits, same hashes, different owner. Then clone your fork to your computer.

In the examples, ~/github/tealab/kettle.git is the original project and a bare clone of it at ~/github/ada/kettle.git is the fork:

bash · try it yourself
# the original project, owned by "tealab"
git init -q --bare ~/github/tealab/kettle.git
git clone -q ~/github/tealab/kettle.git ~/maintainer 2>/dev/null && cd ~/maintainer
printf 'Kettle: a tiny tea timer\n' > README.md && git add . && git commit -q -m "Initial commit" && git push -q

# click "Fork": a copy under your account
git clone -q --bare ~/github/tealab/kettle.git ~/github/ada/kettle.git

# clone your fork
git clone -q ~/github/ada/kettle.git ~/kettle && cd ~/kettle
git remote -v
git log --oneline
origin	/home/learner/github/ada/kettle.git (fetch)
origin	/home/learner/github/ada/kettle.git (push)
efc14ce Initial commit

Adding the upstream remote

Your clone only knows about your fork (origin). To get new commits from the original project, add it as a second remote, conventionally named upstream:

bash · try it yourself
git init -q --bare ~/github/tealab/kettle.git
git clone -q ~/github/tealab/kettle.git ~/maintainer 2>/dev/null && cd ~/maintainer
printf 'Kettle: a tiny tea timer\n' > README.md && git add . && git commit -q -m "Initial commit" && git push -q
git clone -q --bare ~/github/tealab/kettle.git ~/github/ada/kettle.git
git clone -q ~/github/ada/kettle.git ~/kettle && cd ~/kettle

git remote add upstream ~/github/tealab/kettle.git
git remote -v
git fetch upstream
git branch -r
origin	/home/learner/github/ada/kettle.git (fetch)
origin	/home/learner/github/ada/kettle.git (push)
upstream	/home/learner/github/tealab/kettle.git (fetch)
upstream	/home/learner/github/tealab/kettle.git (push)
From /home/learner/github/tealab/kettle
 * [new branch]      main       -> upstream/main
  origin/HEAD -> origin/main
  origin/main
  upstream/HEAD -> upstream/main
  upstream/main
Remote Points to You use it to
origin your fork push your branches
upstream the original project fetch the latest official code

Keeping your fork in sync

The original project keeps moving while you work. Syncing means: fetch from upstream, update your main, push it to your fork (GitHub's Sync fork button does the same on the server):

bash · try it yourself
git init -q --bare ~/github/tealab/kettle.git
git clone -q ~/github/tealab/kettle.git ~/maintainer 2>/dev/null && cd ~/maintainer
printf 'Kettle: a tiny tea timer\n' > README.md && git add . && git commit -q -m "Initial commit" && git push -q
git clone -q --bare ~/github/tealab/kettle.git ~/github/ada/kettle.git
git clone -q ~/github/ada/kettle.git ~/kettle && cd ~/kettle
git remote add upstream ~/github/tealab/kettle.git

# meanwhile, the maintainers add a feature
cd ~/maintainer
printf 'green=120\nblack=240\n' > times.cfg && git add . && git commit -q -m "Add brewing times" && git push -q

cd ~/kettle
git fetch -q upstream
echo "new upstream commits: $(git log --oneline main..upstream/main)"
git merge --ff-only upstream/main
git push -q origin main
git log --oneline --all
new upstream commits: 3d12e3a Add brewing times
Updating efc14ce..3d12e3a
Fast-forward
 times.cfg | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 times.cfg
3d12e3a Add brewing times
efc14ce Initial commit

Now main, origin/main (your fork) and upstream/main all point at the same commit.

Tip: Never commit your own work on main in a fork. Keep main an exact mirror of upstream and do all work on branches; then syncing is always a clean fast-forward.

Fork or branch?

Situation Use
You are a member of the project with write access a branch in the main repository
Open-source project, you have no write access a fork
You want your own long-term variant of a project a fork

Test yourself

What is a fork?

  1. A local branch
  2. A copy of a repository on the hosting server under your account
  3. A merge conflict
  4. A tag

In a typical fork setup, what does the upstream remote point to?

  1. Your fork
  2. The original project you forked from
  3. Your local main branch
  4. GitHub's backup server

How do you bring the original project's new commits into your fork's main?

  1. git push upstream main
  2. git fetch upstream, merge upstream/main into main, then git push origin main
  3. Delete and re-fork
  4. git clone again