Bernie Cook's Blog

Azure, C#, .NET, Architecture & Related Tech News

Adding a Local Git Repository to GitHub: Step-By-Step Guide

2 Comments

GitHubEveryone loves GitHub, and if they don’t it’s only because they haven’t started using it yet ūüėČ

So what do you do if you have a local Git repository and you want to push it up to GitHub to share it with the greater public, or use it as a collaboration point with a number of other developers without spending any money? Well that’s where GitHub comes in, it’s a “web-based hosting service¬†for software development projects that use the¬†Git¬†revision control¬†system.”¬†– Wikipedia. GitHub sports a number of features but we’re only going to be focusing on it’s ability to house a public repository in this post.

If you do want to be wowed by the scale a large-scale project supported by GitHub do check out this visualisation of the Ruby community on GitHub or browse through the Ruby code from their GitHub page.

As a brief aside, this post is preceded by another titled: ‚ÄúGetting Started with Git and Visual Studio: Step-By-Step Guide‚ÄĚ so please check it out if you want to get started with Git and Visual Studio.

Assumptions

Several quick assumptions to tick off.

  1. You’re using a Windows machine – sorry this is targeted at Windows users only
  2. You have Git installed locally as well as Git GUI and Git Bash (available for download from Git for Windows)
  3. You have a local Git repository

Step 1 of 1. Create a GitHub Account

Creating a GitHub account is simple. And free if you’re planning on only creating public repositories. So head over to GitHub and use the sign up form on the home page.

Step 2 of 2. Adding the Project to GitHub

A more Git-compliant way of saying this is “pushing your local project repository to a GitHub public repository”. So essentially you’re taking our Visual Studio solution, which is stored in a local Git repository, and you’re pushing it onto GitHub. Once on GitHub you can push changes to it as you see fit.

I do need to make note at this point that you could use the¬†GitHub for Windows¬†tool or just Git GUI to perform the following steps. Instead I’ve chosen to use a combination of Git Bash and Git GUI so you can get a basic idea of what’s going on under the hood. Git is a very command line-oriented system and it’s useful to know what’s going on behind all the button clicks and drag and drop operations.

Okay, enough said, the first thing we need to do is create a repository on GitHub where we want to push our local repository. So sign into GitHub and go to the new repository page,¬†complete the form and click on the “Create repository” button when you’re ready. The only important things to point out is that you want to keep the “Public” option chosen unless you have a paid account, and ideally it helps to give the repository the same name as the local one.
New Repository
Once you’ve done this you’ll have a fixed repository address, i.e.¬†https://github.com/BernieCook/MvcApplication3Test. This is where you’ll be able to view and manage your local repository once you’ve pushed it up to GitHub.¬†The following page will appear after you’ve created the repository and good old GitHub will even tell you which Git commands you’ll need to execute in Git Bash – see below under the “Push an existing repository from the command line” heading:
Push An Existing Repository
If we were using a GUI application we could get through the next few steps with a series of button clicks but we’re going to take those two Git commands from the screenshot above and run them through Git Bash instead.

    git remote add origin https://github.com/BernieCook/MvcApplication3Test.git
    git push -u origin master

So open up Git Bash and navigate to the root folder of your Visual Studio solution. TIP: An easy way to do this is to use Windows Explorer to find the solution’s root folder then right-click it and select the “Git Bash Here” option. This will open up Git Bash right on top of your folder.
Git Bash Here
Now that we have Git Bash open paste in the two commands (right-click the command line menu – EDIT > PASTE)
Git Bash
You’ll be prompted for your GitHub credentials so type them in and after your password has successfully been entered click your “Enter”¬†key to continue.

Git Bash will now kick off the push operation.
Git Bash
And eventually, depending on the size of your local repository, local bandwidth etc. you’ll have pushed your local repository to your public repository on GitHub. Congratulations!
Git Bash
Now go to your new repository’s address on GitHub and you’ll see all your files.
GitHub
And you’re done, you’ve successfully pushed a local repository up to GitHub!

So What Happens Next?

So what happens now? You’ve pushed a “local repo” (since you’ve gotten this far we can now start using the local Git lingo) to GitHub. Others can browse the code as it’s in a public¬†repo on GitHub. But what happens when you want to push up a new set of changes?

Lets walk through this with a hands-on example. Go and add some files and edit a bunch of others via Visual Studio, then commit them to your local repo.

Now before you go any further take a brief moment to browse through your new GitHub repository to see if the local changes you just made on your local computer have been reflected in GitHub. They won’t be because you haven’t pushed them up yet – which you probably already guessed.

Lets take care of that now with Git GUI. Open it up.
Git GUI
Select the “Open Exising Repository” option
GitGUI2
Browse to the local root folder for your application (Git GUI isn’t looking for a special file, it just wants the folder). Select it and click “Open”

You should see the following window which you can use to review your changes:
GitGUI3
Now to push to GitHub you simply click on the “Push” button or use Ctrl+P. The following window will popup.
GitGUI4
Feel free to leave the defaults as they appear and click “Push”. You’ll be prompted for your username and password (thankfully, as you don’t want someone else pushing their changes over yours).
GitGUI5
GitGUI6
Once they’re filled in and you’ve clicked “OK” you’ll be presented with the following window while the push is performed.
GitGUI7
Success! Now go to your GitHub project folder online and you’ll see the changes reflected.

Congratulations!

Final Note

Becoming a Git expert takes a fair amount of reading and hands-on experience but it’s definitely worth the investment. It’s an an incredibly useful DVCS and is very well complemented by GitHub.

Hopefully this was a informative introduction into the world of GitHub and proves of some benefit as you spend more time working with Git.

Finally, if you don’t like the Git GUI interface, or using Git Bash ,a good alternative is¬†GitHub for Windows. I purposefully chose not to use it here because I wanted to draw attention to the other tools.

Advertisements

Author: Bernie

I currently live and work in the UK, just outside of London. I've been working in IT for 15+ years and in that time have solved many technical problems via blogs, forums, tutorials etc. and feel like I should be giving something back. This blog post is my way of contributing and I hope it proves just as useful to others as their contributions have to me.

2 thoughts on “Adding a Local Git Repository to GitHub: Step-By-Step Guide

  1. very helpful….thank you so much…

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