Here is a helpful beginners tip about managing forked repos in Github.
When you fork a Github repo, it makes a copy of that code for you in a remote location. Often developers work by copying that remote copy to their local machines (called ‘cloning’), and then pushing new changes from their clone to their forked repo. When they are happy, they submit a pull request from their forked repo to the upstream repo and that is where their changes are merged in.
However, what happens when changes are made by other developers to that upstream repo? Your forked repo becomes out of date. If you were looking at a timeline, your copy would be set to to a point of time in the past.
Have you ever wondered how to update your forked repo with the latest changes from its upstream repo? Say you forked a repo and then it sat for 1 month without you making any changes to it. During that time, changes were made to the upstream repo. How do you bring them in sync? Here’s how I do it. The following are Git commands that can be called using Git Bash:
- Start by cloning your forked repo. You may have already done this after you forked the repo. If that is the case, you would have a folder on your machine named <repo name>. Using git bash, if you type “git remote –v” from within your repo, you will see a remote named ‘origin’ with a URL of your forked repo.
git clone <url to forked repo>
- Next, move into the cloned repo.
cd <repo name>
- Next, add a remote for the URL of the upstream copy of the repo. A remote is simply a bookmark to a URL. It’s useful so you don’t have to type out long URLs each time you want to engage a remote repo.
git remote add upstream <url to upstream repo>
- Check to make sure that you have both remotes named ‘origin’ and ‘upstream’. Each should have a push and pull URL:
git remote --v
- Next, pull the latest changes from the upstream remote into your cloned copy of the repo:
git pull upstream master
- Finally, push those changes up into your forked copy. Remember: you cloned your forked repo. So the ‘origin’ remote is pointing to that repo. You may want to ask yourself “Don’t I need to add or commit these changes?” However the answer is “No”, because these are not *your* changes. They are simply going to be forwarded.
git push origin
Hopefully this practice makes more sense now?
In summary, the upstream changes were pulled down into your cloned repo, and then pushed up to your forked repo to bring them in sync.