Forking Workflow
Last updated on 2025-12-03 | Edit this page
Overview
Questions
- What are the common workflows of the forking branching model?
Objectives
- Use the forking workflow to contribute to a project.
Preparation: Make sure that the main is clean, everything is committed.
The forking workflow is popular among open source software projects and often used in conjunction with a branching model.
The main advantage is that you enable people external to your project to implement and suggest changes to your project without the need to give them push access to your project. In this workflow developers usually interact with multiple (at least two) repositories and remotes: the original code repository and the forked repository. It is important to understand that a fork represents a complete copy of a remote repository.
In order to understand the forking workflow, let’s first take a look at some special words and roles needed:
- upstream - Remote repository containing the “original copy”
- origin - Remote repository containing the forked copy
- pull request (PR) / merge request (MR) - Request to merge changes from fork to upstream (a request to add your suggestions to the “original copy”)
- maintainer - Someone with write access to upstream who vets PRs/MRs
- contributor - Someone who contributes to upstream via PRs/MRs
- release manager - A maintainer who also oversees releases
In this workflow changes are suggested and integrated into the “upstream main” branch via a pull/merge request from an “origin branch” (and not the “origin main”). The “origin main” branch is updated by pulling changes from the “upstream main” as they become available.
This way the “upstream main” remains the true source for any suggested change in the project, while allowing anyone to work on their own contributions independently.
Pull requests / merge requests are a service of the hosting platform to ease code reviews and managing changes. They only work within the same hosting service, while you certainly have remotes on several hosting services configured for a repository.
The main workflow
Once you discover a hosted Git project that you want to contribute to, you can create a fork of this repository.
After clicking fork you can choose in which group you want
to fork in. Only groups where you have write access will be listed.
Depending on the branching model of the upstream repository, you may
want to copy only the main branch (or rather the branch set
as the default branch). For most repositories a full copy
will be the best choice, especially if the upstream repository uses a
more complex branching model.
Cloning the forked copy
After the fork is complete, you clone the repository as you would with any other personal project repository. Your copy will then use the remote name origin.
On you cloned working copy you should then create a branch for your proposed changes.
You should avoid changing the main branch of your local
repository, as this should only be your reference to the upstream
main branch, which eventually be providing updates (i.e.,
new commits). If you change your local main branch, its
history will diverge from the upstream’s history of the
main branch causing problems in the future.
Make changes and push branch to your fork
After you cloned the forked repository, you interact with it just the way you would with a repository of your own. In fact, the forked copy now is your own, as you have full access control over the forked project. Remember to only work on branches.
Create a pull request
After pushing
Exercise 1: Full fork-to-pull-request example
Complete the main forking workflow once for your project.
- Fork a repository named by the workshop instructors.
- Clone the repository
- Create a new branch for the changes
- Add and commit a change
- Push branch to origin as the tracking branch
Keeping up-to-date
You may either want to continue to contribute to the forked project, or the forked project has received updates before your changes could be merged. Either way, you will want to incorporate the upstream changes into the corresponding branches both in the forked repository and your working copy. For this it is best to add the original source, the upstream as a remote.
As you never modify the main branch, it remains straight
forward to pull in any change from upstream. First you switch to the
main branch of your working copy, pull in changes directly from
upstream main, and push them to your forked
repository on origin.
Updating your branch is a bit more intricate. Here, we want our
contributed changes to be the last on the branch, so we need to
rebase the branch. When we rebase the branch to the
newest commits on main, the history of the origin
branch and the working copy branch will have
diverged and you will have to update the origin
branch by force.
BASH
git switch mybranch
git pull --rebase upstream main
# resolve potential conflicts
git push --force origin mybranch
Force-pushing to a remote branch will invalidate any other copies of
that branch in other people’s working copies. This, it is usually a bad
idea to force-push on branches actually worked on by others. In that
case, just merge the changes with git pull upstream main
creating a merge commit.
The reason why some people prefer the rebase over the merge is that it keeps the history cleaner.The merge commits It therefore remains a judgement call and will largely be influenced by the specific policies in place for the repositories of the projects you collaborate with.
Exercise 2: Keeping things up-to-date
Update the copies of your main branch after the
successful merge of the pull request.
You can do this with the UI of the hosting service, but try to use the command line.
- The forking workflow allows third parties to prepare and propose changes without write access to the upstream repository
- The
mainbranch is not modified but only be updated from the upstreammainbranch - Branch off
mainto a feature branch, pushing to the forked repository (origin) - Update forked
mainbranch usinggit pull upstream mainwhereupstreamis the name of the upstream remote - Update your local feature branch by
git pull --rebase upstream main - Force push to origin branch for pull request updates.
{alt=A
cropped screenshot of the Github UI showing the array of buttons on the
top right of the project home page with the fork button
highlighted.}
{alt=A
cropped screenshot of the Gitlab UI showing the array of buttons on the
top right of the project home page with the fork button
highlighted.}