Git is a version control system that tracks changes on a file or a set of files among multiple people. It is usually used for source-code management by developers but can just as well be used to track changes on any other files. It was created originally by Linux Creator Linus Torvalds in 2005 during the development of the linux kernel.

Github is an online git repository hosting service. It hosts a huge collection of git repositories. Most of them are open-source. It also offers a service called Github Pages which hosts static sites free of charge.

I. Installation

i. Git on Linux

Installing git on Linux is straight-forward stuff. Simply open your terminal and type:

  $ sudo apt install git
  

ii. Git on Mac

To install git on Mac, you can do either one of two things. The first way to do this is to download XCode which comes with almost all the developer CLI tools you’ll ever need. The second way is simply to download a binary installation. You can find one on the git website. Once the download is done, simply follow the installation steps and you should be good to go.

iii. Git on Windows

Like Mac OS, you can install git on windows using a number of ways. The first is to download the official git build on the Git website. To do so, go to http://git-scm.com/download/win and the download will start automatically. This package will allow you to use git as you would on Linux or Mac and is known as the Git for Windows Project. Once the download is done, open the .exe file and follow the installation steps.

The second way to install git on windows is to use the automated installation using Git Chocolatey Package. This package contains Git Bash(CLI) and Git GUI. Once the download is complete, follow the installation steps and then open PowerShell and type:

  C:\> choco install git
  

iv. Configure git

Configure git by adding your username and email like so:

git config --global user.name "[insert name]"
#don't add the square brackets
git config --global user.email "[insert email]"

II. Github

If you’re new to github and don’t have an account, then create one on the website: https://github.com

With that out of the way, login to your account and create a new repository. When prompted to create a public/private account, choose public. This is because private accounts are paid for on GitHub and you probably don’t want to get into that right now.

Once you have created your repository, you’ll want to link the new repo with your local working environment. This will allow you to work locally and then push changes to your repository on Github via the Terminal/Command Prompt.

To link the repository on your github account to your local working environment, you can use HTTP or SSH. I won’t be getting into SSH in this tutorial, that will be explored in another tutorial. So we’ll use HTTPS for now.

In your terminal/command prompt, create a new folder. This will be our working environment. To do so, type:

mkdir project1

Then initialize the working environment as a git repo:

#Move into your working folder
cd project1
#Then initialize the repo
git init
#You should get output confirming that
#you've initialized a new git repo

The last step is to link the working environment to the remote repo on Github. To do so, copy the link to your Github repo. Then go back to your terminal and type:

git remote add origin [insert the url]
#don't add the square brackets. Then check that
#the remote has been added by typing:
git remote -v
# you should get output displaying remote fetch url and
#remote push url. This means the remote has been added successfully

IV. Add, Commit, Pull & Push

Now that you’ve linked the working environment to your Github repo, you can now add files and push them to Github.

Using your favorite text editor(I recommend Atom), open up your project and add a file. For the sake of this example, call it example1.py. This will be a simple python file. In the file, type something simple like:

def hello():
  print "What is your name?"
  name = raw_input()
  print "Hello,", name

hello()
#this will ask for user input and then print out a greeting message

Now that we have a file, save it and go back to your terminal. Then type:

git status
#This will highlight the file that you've created in red
# We can now stage it to ready it for pushing

i. Git Add

Now that we know the file that we’ve created, we can stage it by typing: Git add + [name of file]

git add example1

ii. Git Commit

When we’ve added a file, we need to commit it to confirm that we’re sure we want to push it. The commit command takes a message argument which explains what we’ve done with the file.

git commit -m "Added file: example1"

iii. Git Push

git push is usually the final command in the git work flow. However there are times when you will need to sync with the work on the remote repository. For our example, we have no files in our repo and therefore we won’t have to sync anything. However if we did have to, here’s what we would type:

git pull
#git pull command only works when we've already specified
# the 'branch' we are pulling from so we do this using
git pull origin master
#sometimes there will be an 'unrelated histories' error.
# To fix this, if you experience it, type:
git pull origin master --allow-unrelated-histories
#if everything goes well, the next time you pull
#all you'll have to do is type that first command

When we’ve committed our changes and are ready to push, the final command is the push command. If we don’t always want to specify what branch to push to, we use the ‘–set-upstream’ argument to tell git that we’re sure we want to always push to the same branch. Anyways, let’s wrap up with:

git push -u origin master
# this is the shorter version of git push --set-upstream origin master
#if you were using gh-pages as your branch,
#you would replace master with gh-pages

On Windows and On Mac OS, it’s possible to use the Github Desktop app to do all of this using a friendly user interface. But doing it in the terminal is just as sweet!

And there we go! That’s a primer on git & github. There’s a lot more to struggle with but this should be a good place to start.

Edit: An earlier version of this article stated that to set default branch using the ‘git pull –set-upstream’ command was possible. However, it is only using the ‘git push’ command that the flag ‘–set-upstream’ is valid.