Git: Syncing a Fork In It

On the second day of Commitmas, I had a new hurdle to jump — I forked @mjbrender’s repo, submitted a PR, and he accepted my changes as well as changes from @joshcoen and now my fork is outdated. How do I update my fork and local repo with the changes?

To rectify this, we’re going to leverage two git commands: git remote add upstream and git fetch upstream command.

With git remote add upstream, we’re going to specify the repository that we forked. In my case, I did:

git remote add upstream https://github.com/mjbrender/12-days-of-commitmas

A git remote -v verifies that we added the upstream source:

Screen Shot 2014-12-22 at 10.12.15 PM

Then we perform git fetch upstream to copy the original repository (the one we forked) to our local repository (on our dev box):

Screen Shot 2014-12-22 at 10.12.23 PM

Now we just fetched the upstream/master branch/repo and needs to be merged into our master branch. We can switch to our master branch with git checkout master.

Finally, git merge upstream/master to merge upstream/master to our master branch.

Screen Shot 2014-12-22 at 10.12.55 PM

The final stretch! Our local repository is now a clone of the original repository again but we need to push our changes back to our repository on GItHub.

Screen Shot 2014-12-22 at 10.28.16 PM

Done! Join in the Commitmas conversation on Twitter using the #vBrownBag hashtag! Happy committing!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s