Working with Personal Forks
Personal forks are a standard open source tool for making pull requests against repos where you donโt have write access. Itโs a little more complicated than working directly in the repo, but itโs not difficult.
There are many git tools, and styles of working. These instructions focus on the git command line. There are other ways to accomplish the same workflow, including more GitHub GUI-centered, or the gh GitHub command-line tool. We welcome improvements to this page.
Terminology
Fork: On GitHub, a fork is a copy of a repo in your own account so that you have write permissions to push commits and branches to it. This is different than the broader open source term of forking a project because of a disagreement about direction. We are talking about the gentle GitHub term, not the aggressive governance term.
Upstream: the repo you are ultimately contributing to. In our world, this is likely in the openedx
GitHub organization.
Remote: a server copy of a repo. A local repo on your machine can have a number of different remotes, and pull from them separately. Each remote has a name and a URL. The default remote is called โoriginโ. You can see your remotes' names and URLs with git remote -v
.
The Ideal State
You will have a local repo with two remotes: โoriginโ will point to your fork in your account, and โupstreamโ will point to the repo in the openedx org. Very briefly, the normal flow will be:
Pull from the upstream into your fork to get the latest code.
Make a feature branch in your repo.
Make your changes, and commit them.
Push your branch to your fork (origin).
Make a pull request against upstream.
Once the review is complete, the owning team will approve and merge the pull request.
Creating a fork
Creating a fork is easy:
Visit the repo on GitHub.
In the upper-right corner is a Fork button. Click it.:
On the next screen, you will be asked where to fork it, called the Owner. Choose your own personal account (the default choice):
If you are a member of the edx organization, it will be listed in the drop-down, probably with โinsufficient permissionโ, meaning you cannot choose โedxโ as the Owner.
If you have permissions to fork into your companyโs organization, you should check carefully with your process owners to see if you should.
The downsides to forking into your organization:
Now there will appear to be two sources of truth:
openedx/repo
andmyorg/repo
. Developers will have to be careful about which to clone, and which to merge into.Thereโs now a shared task of keeping the two repos in sync.
Getting the fork locally
How you get the fork locally depends on whether you already have a local repo copy that you want to keep. If you have a local copy but donโt need it, you can delete the repo directory and use โI donโt have a local copy.โ
A. I donโt have a local copy
Clone the repo locally:
git clone https://github.com/USER/REPO.git
Change to the new repo directory:
cd REPO
Add a new remote for upstream:
git remote add upstream https://github.com/openedx/REPO.git
B. I have a local copy I want to keep
The rename will change the remote of all your branches to upstream, which is not what you want. So, it is probably best to complete your work and remove the old copy, and then go with plan A.
Rename the existing remote:
Use
git remote -v
to check that the openedx repo is named โoriginโRename the remote to โupstreamโ:
git remote rename origin upstream
.
Add a new remote for your fork:
git remote add origin https://github.com/USER/REPO.git
Keeping your fork up to date
You now have two remotes: your fork called โoriginโ and the openedx core repo called โupstreamโ. Before beginning work on a change, you will want to get the latest code from upstream into your fork. Keep in mind that some repos use a โmasterโ branch, others use โmainโ. If you are in a โmainโ repo, replace โmasterโ with โmainโ:
Switch the current branch to master:
git switch master
.Pull down the changes from upstream and update your master:
git pull upstream master --ff-only
If everything has been done right, this will be a fast-forward merge, with no explicit merge commit. You shouldnโt have changes on your master, but if you do, the
git pull
command will fail with โfatal: Not possible to fast-forward, aborting.โ If this happens, you will need to fix your master branch before continuing. (TODO: explain how to do this.)Push the changes to your fork to keep it up to date:
git push
.
Now your master branch is in sync with upstream, both locally and in your fork on GitHub.
Making a pull request
Making a pull request is very similar to the simple one-remote workflow:
Create a branch locally:
git switch -c user/description
.Make your changes and commit them.
Push your branch to your fork:
git push -u origin @
.Make a pull request on GitHub. The base repository should automatically choose the upstream repo.
Review and work on the pull request as usual. You can push new commits to your branch as usual.
Ask the owning team to approve and merge your pull request.
Once your code has been merged, the steps in โKeeping your fork up to dateโ will get you ready for the next iteration of work. You can also use those steps if you need to rebase your pull request to base it on the latest changes on master.
Source material
These pages were helpful:
ย