Keeping your local GitHub Repo in Sync

This post has been republished via RSS; it originally appeared at: ITOps Talk Blog articles.

How many times have you forked a GitHub repository(repo) and worked on your copy of it, made changes, pushed them back to the original and went off to do something else.  Then a couple of weeks later you thought of something else you wanted to contribute but your copy of the repo was out of date and you had to spend time getting it back in line with the original?

 

Yip, that’s me. I’ve tried all sorts of techniques and methods; however, I’ve found a workflow that works for me.  I want to share how I do it with you all, now there maybe other ways of doing this and I’d love to hear them but this is the way I do it at the moment. :smiling_face_with_smiling_eyes:

 

This article assumes that you have Visual Studio Code and Git installed on your local computer.

 

Fork a Repository

The first step is to find a repository (repo) that I want to work on within GitHub.  Then click on the Fork button

Github RepositoryGithub Repository

 

Once the fork has completed GitHub will load your copy of the repository.  The next step is to take a copy of that repo onto your computer for editing, to do that we carry out an operation called Clone.

 

Find the green button that says Clone or Download on your screen.

Github RepositoryGithub Repository

If you click on that button a URL will display, copy that.

 

Now open Visual Studio Code and click on Terminal > New Terminal.

 

You’ll see a console open at the bottom of your screen, in here we want to change to the directory you want to copy the documentation into.  I usually use the Documents folder on my computer for GitHub files, so I type:

 

cd documents

 

And then issue the command

 

git clone **INSERT THE URL YOU COPIED EARLIER**

 

Now go back to the original repo and copy the clone URL from there.

 

Within your terminal console, you need to change into the directory of your clone repo, in my case the directory is called Test so within my Terminal I type

 

Cd .\Test\

 

Now I type

 

Git remote add upstream **INSERT the original repo clone URL**

 

 

 

Sync Github repositorySync Github repository

You can now push and pull from your copy of the repo and also pull (and push if you have contributor rights) to the original repo, which will help later on.

 

Make your contributions

 

Now that you have the repo on your local machine you can make the changes that you want to make, commit them to your repo and then issue a pull request to the original source.

 

Keeping your copy in sync

 

So, it’s maybe been a few days or weeks, since you made your initial fork/clone and you want to make another contribution, but there have been changes to that repo since and your copy is out of date.  To bring your copy in sync, this is the process that I do.

 

Within my terminal within Visual Studio Code I navigate to the location of my repo copy and type

 

Git checkout master

 

This makes sure I am on the right branch of the repo.

 

Then I type

 

Git pull upstream

 

 

 

Sync Github repositorySync Github repository

Which pulls changes from the original repo, meaning any changes the owner of that repo has made or merged from other contributors is now copied on my machine.

 

I can now make the changes I want to, add them to my copy of the repo and then do a pull request to merge with the original.

 

It means I am not working on old files and encounter issues when I try to merge my contributions with the original repo.

 

Summary

 

It’s a workflow that takes a few steps and a few times to remember but I’ve found it works quite well and saves me any issues when I issue my pull requests later on.

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.