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}}
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.
You can verify your remotes with:
git remote -v
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)
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
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
and then pull the matching branch from the upstream
remote:
git pull upstream master
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
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)
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?
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
Yeah you're right. Alright, thanks! :)
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).
Yeah I think I should learn the basics. Thanks for the advice :)
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 ;)
Oh! That's nice. Thanks :)
Man... I watched way too much "The Good Place". For a second I thought you forked up your repo!
I have forked up many a repo in my day for sure!
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.
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 π¨βπ»
Thanks for your kind words!