- Published on
Forking A Repository on Github and Subsequently Creating a Pull Request
- Authors
- Name
- interglobalmedia
- @letsbsocial1
- Link to Forking A Repository on Github and Subsequently Creating a Pull Request on the Original Repository podcast on anchorfm
I little while ago I went over a student's homework project, and I noticed a small error in her html. Since I am also teaching my students Git, I decided that I would add the experience of forking repositories and subsequent pull requests on the original repos. After all, that is at the center of Git collaboration.
- First, I had to fork her repository. I went to the repository in question, and clicked on the fork button to the right of the star button. I wanted to be able to make slight changes where necessary to her repository, but most of all, I wanted her to gain exposure to merging pull requests. And perhaps even forking (open source) repositories herself.
Whenever I fork a repository, my goal is to be able to make pull requests
and offer suggested changes to the original source code. In other words, "propose changes to the upstream", aka "original" repository. Don't we all? And where does this all take place? Via the Command Line
(Terminal) of course! And sometimes, especially in the cases of proposing changes to open source repositories that you may be interested in or even depend on, it is also a good idea to regularly sync your fork with the upstream
repository.
- Next, I needed to create a
clone
locally on my laptop. First I go to myfork
I have created of my student's repository, copied the url of the repository residing inside the "Clone or download" button, and then Igit clone url
it usingSSH
(that is what I am set up for on Github) inTerminal
.
git clone https://github.com/YOUR-USERNAME/name-of-original-repo.git
If you are using https
or
git clone git@github.com:YOUR-USERNAME/name-of-original-repo.git
If you are using SSH
. Now you have your copy of the repository you have forked locally on your computer.
Next I want to configure Git to sync my
fork
with the original repository I forked.- First I go back to the repository I forked.
- Under the repository name, again I click the "clone or download" button and copy the url inside, choosing
HTTPS
orSSH
. - Next I go back into
Terminal
. I cd into theroot
of mylocal copy
of the forked repository. - Then I execute the command
git remote -v
. It shows me the link associated with thegit fetch
andgit push
commands onorigin
. ->git fetch (origin)
andgit push (origin)
. - Then I execute the
git remote add upstream git@github.com:FORKED-USERNAME/name-of-original-repo.git
. This is very similar togit remote add origin git@github.com:YOUR-USERNAME/name-of-original-repo.git
when preparing topush
a newlocal git repository
toremote origin
for the first time. There we also sync ourremote repository
with ourlocal repository
via theremote url
used for cloning as well. - Next, I check to see whether my syncing took on
upstream
withgit remote -v
again. Ifupstream
prints out to theTerminal Console
for bothfetch
andpush
with the correcturls
, then I know I was successful.
- Now I can set up my
local repository
to besynched
with theupstream repository
on Github with a few more Git commands.- First I make sure I am actually in the
local copy
of myforked repo
. - Then I execute the command
git fetch upstream
. - If there were any changes made on
remote
that are notreflected locally
, they will be fetched fromupstream
into thelocal copy
of theforked repository
. - Next I have to make sure I am
checked out
into themaster branch
. If there are no other branches in the forked repository, only a master branch, then I will already be there as would be evidenced by the appearance of "master" in theTerminal
window. But it is still not a bad idea to go through the paces of git checkout master. All that will happen then is that I would be informed that I am already in themaster branch
. - Next I need to
merge
the changes fromupstream/master
into mylocal master branch
. This brings my fork's master branch intosync
with theupstream repository
, without losing my local changes.
- First I make sure I am actually in the
git merge upstream/master
In fact, what I like to do is
git merge upstream/master --no-ff
because I don't like to fast-forward. I like to see all commits before the merge
(in case if there are any, and sometimes there are). --no-ff
means no fast forward
.
- If I want my local forked version to have the same changes as the original repository I forked, I can do a
git pull upstream [branch]
as I would with my own local repositories, just in those cases it would begit pull
( don't need to do more because I execute git push -u origin master when I push a new local repository to remote for the first time). And then topush
to myforked repository
onremote
, I would executegit push origin [branch]
.- I could also choose to do a
rebase
, but I like to avoid that if not necessary. I don't like to rewrite histories unless I have to.
- I could also choose to do a
And that it is it!
Update 9.13.21:
If I want to make changes to my forked repository
on Github so that I can then make a pull request
on the original repository
, I would do the following before pushing changes I have made to my local copy
of my remote forked repository
:
git remote set-url origin git@github.com:yourusername/orignalreponame.git
This is analogous to git remote add origin git@github.com:yourusername/yourreponame.git
when first pushing your local repository
to your remote repository
on Github. Now you are ready to create your first pull request
on the original repository
you forked
! And when you want to push those changes for the first time to your remote forked repository on Github, you would do the following:
git push -u git@github.com:yourusername/orignalreponame.git
After the first push, you no longer have to include -u
in subsequent pushes. It is only necessary the first time you do so.
I will be embedding this episode of Plugging in The Holes along with a transcript in the form of a post on interglobalmedianetwork.com for your hearing and reading pleasure. I will be including the related resource links mentioned in the podcast of course. Always do. Bye for now!