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. Git on Linux
Installing git on Linux is straight-forward stuff. Simply open your terminal and type:
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:
iv. Configure git
Configure git by adding your username and email like so:
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.
III. Link Repo to Local Working Environment
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:
Then initialize the working environment as a 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:
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:
Now that we have a file, save it and go back to your terminal. Then type:
i. Git Add
Now that we know the file that we’ve created, we can stage it by typing: Git add + [name of file]
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.
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:
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:
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.