How to use Git to sync forked repositories

Overview

This post describes how to use git to sync forked repositories.

Background

I frequently need to look up the exact series of commands needed to sync with an upstream repository. This post is primarily for my convenience, but may be useful to others as well.

Fork

Fork the repository by clicking [ Fork ] from your source code repository website. We’ll use SOME-REPO, in this example.

Clone

Create a local clone of your fork and configure git to sync your fork with the original:

1
2
$ git clone https://github.com/YOUR-USERNAME/SOME-REPO
$ git remote add upstream https://github.com/ORIGINAL-USER/SOME-REPO

Contribute

Hack on code in your fork; make Pull Requests / Merge Requests.

Sync

Sync your fork to keep it up-to-date with the upstream repository:

1
2
3
$ git fetch origin -v
$ git fetch upstream -v
$ git merge upstream/master

Simplify

Assign a git alias to perform the sync steps for you:

~/.gitconfig
1
2
[alias]
pu = !"git fetch origin -v; git fetch upstream -v; git merge upstream/master"

Usage:

1
2
3
4
5
6
$ git pu
From https://github.com/YOUR-USERNAME/SOME-REPO
= [up to date] master -> origin/master
From https://github.com/ORIGINAL-USER/SOME-REPO
= [up to date] master -> upstream/master
Already up-to-date.

Summary

Perform the Fork & Clone steps once for each forked repository. Contribute regularly. Sync often.

((( - Party like a rock star! - )))

References

| Github Bootcamp / Fork A Repo
| Github Guides / Forking Projects
| Gitlab / How to fork a project