Keep Github Original Repository and Forked Repo in Sync via the command line
This tutorial looks at keeping a GitHub fork in sync and updated with the original repository, it involves forking a repo from someone else’s account to your GitHub account, making a local copy of your fork via the command line, and then fetching any changes of the original repo and then pushing those changes back to your GitHub fork.
Some terms we’ll be using…
- yourname = Your GitHub account name
- theirname = Original Repo account name
- master = branch we are working on
- the-repo.git = the name of the repo
Forking the Original Repo from Github
First up is to make an actual fork of the original rep to your GitHub account, go to the repo you want and click the Fork button, this will add a copy of it to your Github account, take a note of the branches in use, this guide uses the master branch.
Once you have done that you’ll see the forked repo in your Repositories tab.
Cloning Fork Locally
Next up is to clone your fork locally so you can use the code in your local development environment, you grab the address of your forked repo on GitHub via the clone/download green button click the clipboard to copy the address.
Then crank open your Terminal app and navigate where you want the repo via the command line on your local machine and clone a copy of it…
git clone [email protected]:yourname/the-repo.git
So now you have your fork on GitHub and lets say the branch you are working with is master, the GitHub fork is known as origin/master and you also have your local version of the repo which is the branch known as master.
cd into your new cloned local repo.
Run git remote to see the GitHub fork
git remote -v
origin [email protected]:yourname/the-repo.git (fetch) origin [email protected]:yourname/the-repo.git (push)
You will see the fetch and pull remote addresses of the fork on your GitHub account as above – the next thing to do is to include the original repo so you can pull down any changes it has had since you forked it earlier.
Adding the Original Repo as an upstream remote
So you now have the forked GitHub repo as a remote that you can push changes to but what about updating your local forked repo with any changes/updates from the original repo, you need to add that as an upstream remote…
git remote add upstream https://github.com/theirname/the-repo.git
Then run
git remote -v
origin [email protected]:yourname/the-repo.git (fetch) origin [email protected]:yourname/the-repo.git (push) upstream https://github.com/theirname/the-repo.git (fetch) upstream https://github.com/theirname/the-repo.git (push)
Now you have both the origin(Github forked repo) and upstream (original repo) available
To update your local repo to the original upstream repo – run
git fetch upstream
Then make sure then you are in your correct branch…
git checkout master
To merge your branch with the upstream repo branch, (change the word master if your branch is different) run…
git rebase upstream/master
Finally push the changes to the GitHub repo fork
git push -f origin master
This will then result in the GitHub fork branch being even with the original repo.
But I had Changes done on my local repo
If you had changes done on your local repo the rebase will fail, you’ll need to stash them, then rebase, then reapply them.
git stash
git rebase upstream/master
git stash apply
Then push to your origin master, although now as you had made changes your origin master will be ahead of the original repo.
You also will have to have SSH keys set up to allow passwordless connection – this is the guide here for that.