Sync repos in “Fork workflow”

Step 1: Check current remote repos, typically you now only have your origin repo:

1
2
3
$ git remote -v
> origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)


Step 2: Add upstream repo to track, the repo from which you fork:

1
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git


Step 3: check again your remote repos:

1
2
3
4
5
$ 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)

In case the beginner confused, the “origin” repo is actually the copy version, the “upstream” repo is the true original repo.


Step 4: Let’s say if you have finished a feature and had some commits already, you can’t wait to start a new pull request, but do wait, the repo you forked from may have some updates. Solve the conflicts first, don’t leave them to the original team, they may handle many pull requests every day. So, fetch the upstream first to your local:

1
2
3
4
5
6
7
$ 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


Step 5: Switch to your master (usually it is) branch:

1
$ git checkout master


Step 6: Merge “upstream/master” to your “master” and solve conflicts if it has:

1
$ git merge upstream/master


Step 7: Now you can push all changes to your fork (copy) remote repo, and start a pull request. Cheers!



Leave a Reply

Your email address will not be published. Required fields are marked *