DEV Community

Cover image for I've Forked The Repo, Now What?
Jack Harner πŸš€
Jack Harner πŸš€

Posted on • Originally published at jackharner.com

I've Forked The Repo, Now What?

Syncing Your Repo With the Upstream Repo

Originally posted on JackHarner.com

So, you want to make some changes to an Open Source Repo? Good for you. That's the beauty of Open Source, even just fixing typos in the documentation makes a difference. Today I'm going to walk you through keeping your copy of a repo up to date with changes pushed to the original repo.

This tutorial assumes:

  • You have a basic understanding of git
  • You have a forked repo
  • The original repo has changes that you want in your forked repo

Setting Up The Remote

The first thing you want to do is add the Original Repo as a remote of your Forked Repo. From the command line you want to move into your project directory and add the Original Repo's clone URL as the upstream remote.

cd {{project_name}}
git remote add upstream {{original_repo_url}}
Enter fullscreen mode Exit fullscreen mode

The Repo URL can be found on the Repo page on Github. Click on the Clone or download button and make sure it says "Clone with HTTPS" since you don't have write access to the Original Repo.

Original Repo URL

You can verify your remotes with:

git remote -v
Enter fullscreen mode Exit fullscreen mode

which should output:

origin  git@github.com:{{you}}/{{project_name}}.git (fetch)
origin  git@github.com:{{you}}/{{project_name}}.git (push)
upstream    https://github.com/{{owner}}/{{project_name}}.git (fetch)
upstream    https://github.com/{{owner}}/{{project_name}}.git (push)
Enter fullscreen mode Exit fullscreen mode

Go Get That Code!

Now that we've associated our Original Repo with our local Forked Repo, it's time to go get the new code.

git fetch upstream
Enter fullscreen mode Exit fullscreen mode

Running git fetch gets the changes to the remote repo without changing any of the files in your local version. It essentially lets you see what everyone else has done to the code without making any changes to yours. You should see it pull down a bunch of different branches, tags, and some file changes.

To apply the new changes to your repo make sure you're on the branch you want to pull in (in our case master):

git checkout master
Enter fullscreen mode Exit fullscreen mode

and then pull the matching branch from the upstream remote:

git pull upstream master
Enter fullscreen mode Exit fullscreen mode

It will asks you for a message for the merge commit. You can usually just leave the default and just save with CTRL + X. Git finishes up the merge commit, and your Local Repo is now up to date with the Original.

Seal The Deal

Finish off your changes by pushing them back up to your Forked Repo's origin remote that you have write access to.

git push origin master
Enter fullscreen mode Exit fullscreen mode

Ta da! You just refreshed your forked repo. Make, test, commit and push your changes and you'll be all set to create a pull request back to the Original Repo. You're ready to start changing the world of Open Source Software one pull request at a time!

Top comments (12)

Collapse
 
ltosh9802 profile image
Toshik Langade

I'm a beginner and I'm wondering is there any need to learn git? With Github Desktop we can do basic things very easily without remembering those syntaxes. Should I learn git or not? What are your thoughts?

Collapse
 
tkdmzq profile image
TKDMzq

sometimes you dont have luxury of desktop version of git it is usualy there + it acustomes you to terminal witch you use very often for many things i.e. working on remote production server

Collapse
 
ltosh9802 profile image
Toshik Langade

Yeah you're right. Alright, thanks! :)

Collapse
 
jackharner profile image
Jack Harner πŸš€

Personally, learning git in the command line was a very helpful introduction to using the command line. When I was first learning git, GitHub Desktop was still pretty new and all the tutorials I could find about git practices were just using the command line. I'm sure the landscape has changed in the past few years as GitHub Desktop keeps getting improved, but being familiar with the command line expands the amount of stuff you can do (outside of git).

Obviously don't let the fear of the command line keep you from making stuff, use whatever works for you. There's plenty of time for learning the rest later (if you want to).

Collapse
 
ltosh9802 profile image
Toshik Langade

Yeah I think I should learn the basics. Thanks for the advice :)

Collapse
 
dirtycode1337 profile image
Dirty-Co.de

Personally I'd recommend learning git at command line level - at least some basics, they helped me a lot understanding git. I've seen many helpful hints from the command line which I wouldn't have seen when using a GUI.
Additionally I got a little fan of SourceTree when I started using GitFlow then :) - so I am now combining command line and GUI ;)

Collapse
 
ltosh9802 profile image
Toshik Langade

Oh! That's nice. Thanks :)

Collapse
 
tiguchi profile image
Thomas Werner

Man... I watched way too much "The Good Place". For a second I thought you forked up your repo!

Holy forking shirt

Collapse
 
jackharner profile image
Jack Harner πŸš€

I have forked up many a repo in my day for sure!

Collapse
 
necrogami profile image
Anton C. Swartz IV

gist.github.com/EvanDotPro/1506822

This is the shell script i've used for years to easily manage that. Add it to your .bashrc or somewhere in your source collection for bash and then do gittyup for a given repo and it just runs.

Collapse
 
varunbarad profile image
Varun Barad

I wrote on the exact same concept a few weeks back. But your article is much more nicely written than mine.
Thanks for writing this πŸ‘¨β€πŸ’»

Collapse
 
jackharner profile image
Jack Harner πŸš€

Thanks for your kind words!