loading...

Git : Sync forked repo with original repo

gsavitha profile image Savitha Gollamudi ・2 min read

Why should I do this? 😏

Before submitting any pull request, syncing your forked repository with original repository is an important step to perform, as you may want to get the bug fixes or additional features to merge with your code since the time you forked the original repo.

But I can create a PR and make changes myself instead.. 💁

You can, but that adds an extra commit into your forked repo instead of matching it with the original repo.

Inorder to sync without any additional changes as a part of the process,

  • Configure the original repo as upstream remote.
  • Merge changes from original repo
  • Push the merged version back to Github.

Adding original repo as an upstream

  • Open the forked repo in your Git Bash or command prompt or terminal.
  • List the current configured remote repositories
git remote -v
> origin  https://github.com/<YOUR_USERNAME>/<YOUR_FORK>.git (fetch)
> origin  https://github.com/<YOUR_USERNAME>/<YOUR_FORK>.git (push)
  • Add the original repo as upstream repo
git remote add upstream https://github.com/<ORIGINAL_OWNER>/<ORIGINAL_REPOSITORY>.git
  • Verify the new upstream repo for your forked repo
git remote -v
> origin    https://github.com/<YOUR_USERNAME>/<YOUR_FORK>.git (fetch)
> origin    https://github.com/<YOUR_USERNAME>/<YOUR_FORK>.git (push)
> upstream  https://github.com/<ORIGINAL_OWNER>/<ORIGINAL_REPOSITORY>.git (fetch)
> upstream  https://github.com/<ORIGINAL_OWNER>/<ORIGINAL_REPOSITORY>.git (push)

You can now pull the changes from original repo.

Merge changes from upstream

  • Open the forked repo in your Git Bash or command prompt or terminal.
  • First things first, fetch the changes (branches and their commits) from upstream
git fetch upstream
> remote: Counting objects: 75, done.
> remote: Compressing objects: 100% (53/53), done.
> remote: Total 62 (delta 27), reused 44 (delta 9)
> Unpacking objects: 100% (62/62), done.
> From https://github.com/<ORIGINAL_OWNER>/<ORIGINAL_REPOSITORY>
>  * [new branch]      master     -> upstream/master

Note : Commits to the original repo(master) will be stored in a local branch, upstream/master

  • Make sure you are on your local (fork's) master branch
git checkout master
> Switched to branch 'master'
  • The last step, which achieves our goal: Merge changes from original repo (upstream/master) into your forked repo(master).
git merge upstream/master
> Updating a422352..5fdff0f
> Fast-forward
>  ...

The above step brings changes of forked repo in sync with original repo, without losing any uncommited changes :D

Optional Step

If you made changes to your repo and want to push them back to Github

git push origin master

To sum up:

These are the only commands you need to sync your forked repo with original repo 🚀

git remote add upstream https://github.com/<Original Owner Username>/<Original Repository>.git
git fetch upstream
git checkout master
git merge upstream/master
git push

Source : Syncing a fork

Posted on by:

Discussion

pic
Editor guide
 

You can also do this to get your fork to match the upstream especially if you accidentally committed and want to get a clean fork

git checkout master 
git fetch --all # master and upstream
git reset --hard upstream/master

Make sure you stash work in progress. The hard flag will restore clean files and the reference to upstream will move your repo to be ahead or behind so it is in sync.

I also use the merge approach you suggested.

 

You can automate a sync using GH Actions

Unfortunately it makes a PR so you'll get a merge commit

Maybe there's a cleaner way to do it that uses the merge approach with no commit

github.com/TG908/fork-sync