Fork and Pull Request Workflow
This document describes how developers may contribute pull requests to
an upstream repository and how upstream owners may merge pull requests
from contributors according to the very popular fork and pull request
workflow followed in many projects on GitHub.
The download buttons above download version 0.6.0
(the latest stable release) of this document.
The most recent version of this document is available at
git.io/gitpr.
Contents
Introduction
Every project has a main development branch where the developers push
commits on a day-to-day basis. Usually, the main development branch is
master but some projects choose to have develop or trunk or
another branch for day-to-day development activities. We refer to this
main development branch as the main development branch throughout this
document to keep the text general. However in the command examples and
ASCII-diagrams, we use master as an example of the main development
branch.
We use the following placeholders in the command examples and
ASCII-diagrams in this document:
GITHUB: github.com or domain
name/hostname of your private GitHub Enterprise system.
USER or CONTRIBUTOR: The user that forks an upstream repository,
creates pull requests, and sends them to the upstream repository.
UPSTREAM-OWNER: Owner of the upstream repository. This is the name
of the user or organization that merges pull requests into the
upstream repository.
REPO: Repository name.
FILES: One or more filenames to be staged for a commit.
TOPIC-BRANCH: Feature-specific or bug-specific branch where a
contributor develops her or his contribution. This is referred to as
the topic branch in the text.
These placeholders should be substituted with appropriate values while
executing the commands presented in this document.
Beginners to this workflow should always remember that a Git branch is
not a container of commits, but rather a lightweight moving pointer that
points to a commit in the commit history.
A---B---C
↑
(master)
When a new commit is made in a branch, its branch pointer simply moves
to point to the last commit in the branch.
A---B---C---D
↑
(master)
A branch is merely a pointer to the tip of a series of commits. With
this little thing in mind, seemingly complex operations like rebase and
fast-forward merges become easy to understand and use.
The next section, Quick Reference, provides a brief
summary of all the frequently used commands involved in creating and merging
pull requests.
Quick Reference
Here is a brief summary of all the commands used to create and merge
pull requests in this document. This section serves as a quick
reference.
# CREATE PULL REQUEST
# ===================
# Fork upstream and clone your fork.
git clone https://GITHUB/USER/REPO.git
cd REPO
git remote add upstream https://GITHUB/UPSTREAM-OWNER/REPO.git
git remote -v
# Work on pull request in a new topic branch.
git checkout -b TOPIC-BRANCH
git add FILES
git commit
git push origin TOPIC-BRANCH
# Go to your fork on GitHub, switch to the topic branch, and
# click *Compare & pull request*.
# Keep your fork's main development branch updated with upstream's.
git checkout master
git pull upstream master
git push origin master
# Amend last commit (optional).
git add FILES
git commit --amend
# Rebase topic branch on the main development branch (optional).
git checkout TOPIC-BRANCH
git rebase master
# Edit commits, e.g., last 3 commits in topic branch (optional).
git checkout TOPIC-BRANCH
git rebase -i HEAD~3
# Force push rebased/edited commits to the pull request (optional).
git push -f origin TOPIC-BRANCH
# Delete topic branch branch after pull request is merged.
git checkout master
git branch -D TOPIC-BRANCH
git push -d origin TOPIC-BRANCH
# MERGE PULL REQUEST (WITHOUT MERGE COMMIT)
# =========================================
# Clone upstream repo.
git clone https://GITHUB/UPSTREAM-OWNER/REPO.git
cd REPO
# Keep the local main development branch up-to-date.
git checkout master
git pull
# Pull changes in pull request into a temporary branch.
git checkout -b pr
git pull https://GITHUB/CONTRIBUTOR/REPO.git TOPIC-BRANCH
# If the above command does not create a new merge commit, ignore
# this comment and follow the remaining commands after this comment.
#
# If the above command creates a new merge commit, first consider
# that it is okay to have a merge commit. However, if you want to
# get rid of it, choose exactly one of the following three options:
#
# - Request the contributor to rebase the topic branch on the main
# development branch. After the contributor has done so, delete
# the temporary branch, start over, and pull the changes again.
# (Commands: git checkout master; git branch -D pr)
#
# - Go to the pull request page on GitHub, click on the dropdown
# menu next to 'Merge pull request', select 'Rebase and merge',
# and click 'Rebase and merge'.
#
# - Rebase the topic branch on the main development branch. Then
# follow the commands below. Finally close the pull request on
# GitHub manually. (Command: git rebase master)
# Merge pull request and delete temporary branch.
git checkout master
git merge pr
git branch -d pr
# Push the updated main development branch to upstream repo.
git push origin master
# MERGE PULL REQUEST (WITH MERGE COMMIT)
# ======================================
# Clone upstream repo.
git clone https://GITHUB/UPSTREAM-OWNER/REPO.git
cd REPO
# Keep the local main development branch up-to-date.
git checkout master
git pull
# Pull changes in pull request into a temporary branch.
git checkout -b pr
git pull https://GITHUB/CONTRIBUTOR/REPO.git TOPIC-BRANCH
# Merge pull request and delete temporary branch.
git checkout master
git merge --no-ff pr
git branch -d pr
# Push the updated main development branch to upstream repo.
git push origin master
The next two sections, Create Pull Request
and Merge Pull Request, elaborate these
commands in detail.
Create Pull Request
This section is meant for developers who contribute new commits to the
upstream repository from their personal fork.
Fork and Clone
On GitHub, fork the upstream repository to your personal user account.
Then clone your fork from your personal GitHub user account to your
local system and set the upstream repository URL as a remote named
upstream.
git clone https://GITHUB/USER/REPO.git
cd REPO
git remote add upstream https://GITHUB/UPSTREAM-OWNER/REPO.git
git remote -v
Now the remote named upstream points to the upstream repository and
the remote named origin points to your fork.
Work on Pull Request
Work on a new pull request in a new topic branch and commit it to your
fork. Remember to use a meaningful name instead of TOPIC-BRANCH in the
commands below.
git checkout -b TOPIC-BRANCH
git add FILES
git commit
git push origin TOPIC-BRANCH
Create pull request via GitHub web interface as per the following steps:
- Go to your fork on GitHub.
- Switch to the topic branch.
- Click Compare & pull request.
- Click Create pull request.
Wait for an upstream developer to review and merge your pull request.
If there are review comments to be addressed, continue working on your
branch, commiting, optionally rebasing, amending, squashing, and
dropping them, and pushing them to the topic branch of origin (your
fork). Any changes to the topic branch automatically become available in
the pull request.
In the fork and pull request workflow, a contributor should never commit
anything to the main development branch of personal fork. This makes it
very easy to keep the main development branch of your fork in sync with
that of the upstream repository. This is explained in the next
subsection.
Keep Your Fork Updated
As new pull requests get merged into the upstream's main development
branch, the main development branch of your fork begins falling behind
it.
The commands below show how to update your fork's main development
branch with the new commits in the upstream's main development branch.
git checkout master
git pull upstream master
git push origin master
The git pull command above simply fast-forwards the main development
branch from an earlier commit to the last commit in the upstream's main
development branch.
When your main development branch falls behind the upstream's main
development branch, the upstream's main development branch extends
linearly from the last commit in your main development branch, provided
that there are no additional commits to your main development branch
that has caused it to diverge.
E---F---G (TOPIC-BRANCH)
/
A---B---C---D (master)
\
H---I---J (upstream/master)
With such a commit history, when the upstream's main development branch
is merged into your main development branch, the merge is done by simply
fast-forwarding the pointer of your main development branch to the last
commit in the upstream's main development branch.
E---F---G (TOPIC-BRANCH)
/
A---B---C---D
\
H---I---J (upstream/master, master)
After the merge is complete, the upstream's main development branch and
your main development branch point to the same commit.
Amend Last Commit
This is an optional step to rework on the last commit. After commiting
some work, one may realize that some files need to be modified or the
commit message needs to updated.
E---F---G (TOPIC-BRANCH)
/
A---B---C---D (master)
Git allows us to pick the last commit, modify the changes made in that
commit including the commit message, and reapply the changes along with
the modifications as a new commit that replaces the last commit.
E---F---G' (TOPIC-BRANCH)
/
A---B---C---D (master)
To do so, first rework on the files and make the necessary changes. Then
add, remove, move, or rename files to stage them for commiting. Finally,
amend the last commit. The commit message may be modified when the
editor comes up to show the commit message.
git add FILES
git commit --amend
Although the above example shows only the git add command to add new
or modified files for commiting, one may amend the last commit by
removing, moving, and renaming files with the git rm and git mv
commands before amending the last commit.
To update only the commit message without changing files, run only git commit --amend.
Rebase Commits
This is an optional step to keep the commit history as linear as possible.
The main development branch may have diverged since the topic branch was
created.
E---F---G (TOPIC-BRANCH)
/
A---B---C---D (master)
It may be a good idea to move the commits in the topic branch and place
them on top of the main development branch, so that the topic branch
extends linearly from the last commit in the main development branch.
E'--F'--G' (TOPIC-BRANCH)
/
A---B---C---D (master)
The following commands show how to rebase the topic branch on the main
development branch.
git checkout TOPIC-BRANCH
git rebase master
Edit Commits
This is an optional step to keep the commit history clean and concise.
A pull request may contain multiple commits. Sometimes one may need to
amend a commit that is not the last commit in the pull request. Commits
prior to the last commit can be amended with the interactive rebase
command.
After developing the required feature or bug-fix in a topic branch, the
developer or a reviewer may notice issues in the work that need to be
addressed before the pull request can be merged into the upstream
repository. This may lead to multiple new commits in the topic branch
that should ideally have been part of the first commit that implemented
that feature or bug-fix.
E---F---G (TOPIC-BRANCH)
/
A---B---C---D (master)
In such cases, it may be a good idea to squash multiple commits in the
topic branch into a single coherent commit with all changes for the
feature or bug-fix being developed.
E' (TOPIC-BRANCH)
/
A---B---C---D (master)
The following example shows how to amend the last 3 commits.
git checkout TOPIC-BRANCH
git rebase -i HEAD~3
This brings up an editor with three lines for the last 3 commits ordered
from earliest to last followed by instructions on how to edit these
lines to amend the commits.
For example, to squash all three commits into one, leave the first
commit untouched, replace pick with squash in the next two lines,
save, and quit the editor. This brings up the editor again. Clean up the
commit message, save, and quit the editor.
Apart from pick and squash, there are other operations such as
reword, edit, drop, etc. to amend or drop commits. Follow the
instructions that appear in the editor to use them. When a rebase
operation involves multiple steps, Git walks you through each step by
offering suggestions and commands that you need to use in each step.
Force Push