Quick and Dirty guide to Github

If you want to look and sound like a real programmer it's time to learn about version control and git. Let's assume you've created a GitHub account and have followed the proper procedure to get an SSH key set up - we're assuming that because that's not what this post is about. Now, back to sounding like a real programmer. I'm also going to assume you're using OSX, because, programmer.

Version control: that's what we do to make sure we're not accidentally overwriting important or working code with new or experimental code. Kind of like that time your brother recorded his bass guitar audition over your only copy of Back to the Future. T·T It's more complicated than that but you get the idea.

First things first. Open "Terminal" and navigate to your project, for example $ cd ~/Documents/myNewProject. (Side note: when I started working in the terminal I initially made the mistake of copying terminal commands with the $ included. That's not going to work; it's just used to illustrate the terminal prompt.)

Git Jiggy Wit It!

We'll start with getting our repo (repository) up and running and then with basic commands to manage branches and versions.

Initiate a repo

The first thing we want to do is initiate a git repository for our local project. This also creates a hidden .git file in our folder.

$ git init

Create a remote repo

This command is used to make a copy of our local repo in our GitHub account. Add your project name at the end to give the remote repo a name.

$ hub create myProject

Add your latest changes

Before committing we need to let git know we've made some changes. These change will always be against the branch we're currently working on.

$ git add .

Commit your changes

It's time to commit our changes to the current branch. Use the "-m" flag to add a comment; helpful if we need to fall back to a previous version or find some code we might have changed.

$ git commit -m "i made some codes"

Push that baby

Once we've committed we need to push the changes to GitHub. Don't worry, all commits are saved, so we won't overwrite anything - yay, version control! "Origin" is GitHub's git name, and "master" is the branch we're working on. It could also be called "experimental" or "bananas"; probably choose something fairly descriptive though.

$ git push origin master

That's it for the basics. Rinse and repeat.

Deeper into the Git tree

Git's more powerful than that though. Let's take a look at some other things we can do.

Clone a remote repo

What if I already have a remote repository and I want to work on it locally? Simple; send in the clones! Open the project in GitHub and grab the "Clone or download" url - it'll look something like this git@github.com:yourUsername/yourProjectName.git

$ git clone git@github.com:yourUsername/yourProjectName.git

Connect your local and remote repo

Sometimes you've already created a local repository and a remote repository. How do you connect those? Use the remote command with add.

$ git remote add origin git@github.com:yourUsername/yourProjectName.git


Git allows us to create branches of our project, so we can work in parallel. Sometimes we want to go in a completely different direction, or add some test features without touching our main branch. Sometimes we want to work together on a project without getting in each other's way. You've got it code monkey - let's make a branch (-b flag) and start working on it immediately.

$ git checkout -b bananas

What just happened? We just created a new branch called bananas and changed our working version to it. Git's checkout seems to create some confusion, but if you think about it like a library the version you're currently reading is the version we checked out last.


One thing you need to know is that git actually moves the working files around in your file system. If you do a checkout in terminal the files that are open in your editor are also swapped out. You might think you have the old version open, but you're looking at the checked out branch. It's a little bit of a mind bender at first.

Checkout that branch

If you're done working on your current branch and want to move to another branch you'll have to make some commits and run git add . and git commit before proceeding. Let's do it.

$ git add .
$ git commit -m 'added more bananas'
$ git checkout master

Where's my branch?

Just like your master branch, other branches must be pushed to be available remotely.

$ git push origin bananas