Skip to content

Instantly share code, notes, and snippets.

@corinneling
Last active February 5, 2023 15:23
Show Gist options
  • Save corinneling/c027da69442ea08c5e67e71f72afe3c8 to your computer and use it in GitHub Desktop.
Save corinneling/c027da69442ea08c5e67e71f72afe3c8 to your computer and use it in GitHub Desktop.
How to sync up a forked repository

How to Sync A Forked Repo

One problem I had while practicing git was syncing up a forked repository. This can come in handy for open source projects and collaborating in teams without permissions to directly push onto an original repo. I'm interested in open source, so I figured it would be a good thing to learn, and a good thing to document (because I am sure the details will skip my mind later).

  1. Make sure you are in the right place

    • That was my first mistake. I was in the wrong directory so I got this error when I tried to merge.

      fatal: refusing to merge unrelated histories

    • That happened because I was telling git to merge one repo (my dotfiles repo) with the other (my apprenticeship repo). Here's some documentation on that error

    • An easy way to check where you are is the command:

      $ pwd

  2. Check your remote repos

    • While your remote repo is a clone of the one on GitHub, it is still considered its own repo.

    • Use the command:

      $ git remote -v

    • You should see this output:

      origin <url of your forked repo> (fetch)

      origin <url of your forked repo> (push)

  3. Then you can set upstream

    • If you want to you can just fetch and then push, but then you will have to do the below commands every time your forked repo is out of sync

      $ git fetch <url of original repo>

      $ git push

    • Setting upstream is an easier way of doing this, which can be good if you are going to be working on committing to the repo a lot.

    • Set upstream command:

      $ git remote add upstream <url of original repo>

  4. Check where you are again

    • To understand what was happening, and to help me make sure I wasn't making the same mistake as before I checked my remote repos with:

      $ git remote -v

    • It should output

      origin <url of your forked repo> (fetch)

      origin <url of your forked repo> (push)

      upstream <url of original repo> (fetch)

      upstream <url of original repo> (push)

    • I don't know why it has the fetched and pushed version of each

  5. Now every time you need to sync up your fork with the original repo all you have to type is

    $ git fetch upstream

    $ git merge upstream/master

  6. To make the process easier you can also set up aliases in your dotfiles

@df-a
Copy link

df-a commented Oct 4, 2020

Hi there, thanks for this.

@Lemorz56
Copy link

Lemorz56 commented Nov 5, 2020

Hi! Just wanted to thank you a lot for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment