I use Git almost daily but I need my memos for the uncommon operations. Therefore the posts:
- Part 1: Git Configurations
- Part 2: Using Forks in GitHub (this post)
This is a short memo about using a forked repository in GitHub to submit a pull request (PR) to the upstream repository.
First of all, I have already forked the upstream repository to my GitHub account by using the Fork button in the repository. In this case I have forked the NetBox repository.
When I go to my fork repository I can use the green Code button to copy the SSH path for cloning my fork:
(As background information, I have created an SSH key pair, saved the public key in my GitHub account and set up KeeAgent with KeePass, as well as configured PuTTY to allow SSH agent forwarding. With this setup, whenever SSH keys are requested on my Linux host I can use the private SSH key stored in the KeePass database on my Windows computer, without copying the private key on the Linux host.)
On my Linux host I will clone the repository for local editing:
cd devel git clone email@example.com:markkuleinio/netbox.git git checkout -b important-changes
Here I already created a new branch (“important-changes”) for my changes. At this point I can do whatever development I like in my fork.
After having committed my changes in the local repo, I can push the changes to GitHub:
git push -u origin important-changes
--set-upstream) option is needed because this is a new branch.
At this point GitHub notices that this can be used as a PR:
Clicking the Compare & pull request button will get me to the upstream repository to create the PR.
Now, this was a straightforward example where I forked the repo and made the changes at one sit. In reality the development process is longer and the upstream repository may have changed meanwhile. To synchronize the fork with the upstream repository, this is how it goes:
git remote add upstream https://github.com/netbox-community/netbox.git git remote -v git fetch upstream git merge upstream/develop
The new remote is only needed to be added once, and the
-v option can be used to check what’s configured already.
“upstream/develop” in the merge command means the “develop” branch in the upstream repository to be merged in. In NetBox the default branch is “develop” but in many other cases it can be “main” or “master”.
Another way to get synchronized with the upstream repository is to use the Fetch upstream button in GitHub:
After doing that a
git pull is usually needed on the local repository (not needed in this case as there were no additional changes in the upstream repo).