GitHub Desktop version information

Sync local repo with the original on GitHub (Git merge upstream)

This post was most recently updated on December 27th, 2022.

3 min read.

This is one of those “note to self” -kinds of entries. This workflow is probably so natural to a lot of you all, that you won’t need to ever google it – but since I don’t do that much development using git with the full “fork – clone – branch – submit pull request” -process (which is really typical with GitHub – and I guess Open Source in general), I always need to look up the instructions on how to add pull any changes from the original repository to yours.

I even need to do that often enough, that I wanted to document it somewhere where I can find it easily. Like my blog :)

So, here goes:

Synchronizing your local changes with the upstream repository

While easy in principle, there are a few steps to remember – see below for details!

Time needed: 10 minutes

How to synchronize your forked and local repositories with the original one on GitHub?

  1. Open a command prompt

    Open Git Bash or similar command prompt with git executable available. Shouldn’t really matter what your terminal or command prompt is called as long as git executable is added to your PATH.

  2. Change the current working directory to your local project.

    Now we’ll change the current working directory to be your project folder. This can be done by running the following command:

    cd [your_project_directory]

  3. Change to your desired branch

    Changing to our default/compare branch is going to be our next hurdle. You’ll probably want to merge to your main or master – so make sure it’s checked out!

    You can change it from GitHub Desktop (if you’re using it), or you can run this:

    git checkout [branch_name]

  4. Configure the origin as a remote repository

    Now we’ll need to add the “origin” of the repository as a “remote repository” to your local git. This needs to be done to enable you to fetch the new commits from it. You can verify if it already is by running this:

    git remote -v

    The remote repository’s github.com url should be displayed in the list. If it isn’t, then run this:

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

  5. Sync your local repository with the upstream (the original one)

    Now we’ll sync our local changes with what’s new in the origin repo. Run the following command in the command prompt:

    git fetch upstream

  6. Perform merge

    Now we’ll merge! Run this:

    git merge upstream/master

    If you get a text editor window in your bash, that’s just Vi asking for your commit comment for your merge. Don’t have to enter anything, just write “:wq” and you should be good.

    If you’re on Windows, install the latest git version and it’ll actually let you set Visual Studio Code as the default text editor.

  7. Push your local changes to your repository

    “Your local changes” = your merge from upstream = all of the new stuff from the original repository on GitHub.

    You can perform this step in GitHub Desktop, or by running this in your console prompt:

    git push

And now you should be good!

Footnotes

To sync local changes back to the GitHub repository you run three commands, what is the final command?

From the statistics, I’m seeing tons of you flocking to the site to look for the answer to this particular question. So let me give you the short and sweet:

git push

No big surprise there, right? 😁

What is “git sync” then?

Git sync does not actually synchronize anything with the origin repository. It doesn’t know how to do that – instead it just performs “git pull” and then “git push” to synchronize with your remote repository – the one you have probably forked on GitHub.

If you aren’t working in a fork, you can actually forget all of the nonsense about git origin repositories – you don’t need to “sync” to the repository then. You’re good with git sync :)

And now, you should be even better!

References

mm
5 7 votes
Article Rating
Subscribe
Notify of
guest

4 Comments
most voted
newest oldest
Inline Feedbacks
View all comments