After months of deliberation, you’ve finally taken the big leap: you’re learning how to program through one of our amazing programming for beginners courses. Things are going great – you understand the syntax, you’ve worked through dozens of examples, and you’ve even created a few simple programs on your own. Encouraged by your progress, you’ve even decided to install Ubuntu and code like a real programmer.
Except now, the course asks you to install Git on Ubuntu and you have no idea what to do. Whatever you know about Git is through stories about GitHub on TechCrunch, and through conversations with your programmer friends who can’t stop raving about it.
Fret not; this tutorial will teach you everything you needed to know about Git and how to install it on Ubuntu.
What is Git?
If you’ve just started out programming, all your programs probably consist of a few dozen lines. Since you are the only programmer and the programs themselves are tiny, keeping track of changes is quite easy. You can even create different versions of the same program easily by copy-pasting code into different files.
But what if you were working on something more substantial, like a web app? How do you keep track of changes on projects employing dozens of programmers?
This is where Git comes into the picture.
Git is an open source Version Control System (VCS). Think of it as a tool that keeps track of any changes made to your own and allows you to revert to an earlier state should things go wrong. Using Git, you can review changes made to a file over time, check who made changes to the file and when, revert a file to an earlier state, revert an entire project to an earlier state, etc. To put simply: no serious programmer can do without Git.
Plenty of alternatives to Git exist, including Apache Subversion, CVS and Mercurial. However, none of these can compete with Git’s speed, efficiency and distributed nature (i.e. code is held in different branches, not a single centralized repository), all of which have combined to earn Git a special place in the programmer community.
GitHub, a social network for programmers that provides an attractive front-end to access Git repositories has also been instrumental in increasing Git popularity. GitHub allows programmers to collaborate on projects and access repositories from a rich visual interface instead of dealing with clunky command lines and terminals. Thanks to GitHub, Git is now the de-facto VCS for startups, web/app developers, and even big companies like Google and Facebook. In fact, GitHub alone boasts over 5.8 million users who’ve uploaded over 12.5 million repositories online!
Installing Git on Ubuntu
Git’s Linux roots run very deep – it was created by Linus Torvalds, the man behind Linux itself. Installing Git on Ubuntu, therefore, is quite straightforward, especially if you are already familiar with the Linux Terminal.
Note: This tutorial assumes you are using Ubuntu v12.04 and have a basic familiarity with the operating system.
Start Ubuntu and open the Terminal on the home screen. You can find it in the left pane as shown below:
If you are new to Ubuntu, the Terminal might be a bit intimidating. Unlike Windows or OS X, where you hardly ever need to go into the command prompt, Ubuntu and other Linux distributions require you to use the Terminal extensively to perform most tasks, including installing software.
As we will see below, using the Terminal to install programs is actually quite easy once you understand the basics.
Type out the following command into the Terminal:
sudo apt-get install git
You can see an example below:
This is the basic command to install Git in Ubuntu. Let us break it down to understand it better:
- Sudo: Sudo stands for “super user do”. This command allows you to act as a “super user” (or ‘root user’) and perform actions normally reserved for the super user, like installing software. Think of it as granting administrative privileges to a user on Windows. You will use this command frequently to perform advanced tasks in Ubuntu.
- Apt-get: ‘Apt’ in this command stands for ‘Advanced Packaging Tool’. The apt-get command essentially instructs Linux to get a package and ready it for installation, updates, etc.
- Install: This should be quite straightforward – it simply tells Linux to install the mentioned software package (in this case, Git).
After you hit Enter, Linux will automatically retrieve and install the latest version of Git. Once done, your Terminal should look something like this:
This is pretty much it – you have now officially installed Git – all in one single command!
We will now learn how to create a Git repo and make a Readme file below.
Note: If you get a dpkg error: dpkg status database is locked by another process message, it means that there is another program running in the background that is interfering with the setup process. Shut down all background processes and try the install command again.
We will now create a new directory that will store our Git repository. To do this, type in the following command into the Terminal:
This will create a directory named “HelloWorld” in your default directory (usually /home/USER_NAME/)
Now switch to this directory by using the “cd” command.
To create a Git repo here, simply use the “git init” command:
This will create a Git repository in the HelloWorld directory.
Now that we have created our Git repository, we can create our Readme file to show how the Git system works.
Make sure that you are in the HelloWorld directory, and then enter the command below:
If you check the HelloWorld directory, you should see a blank Readme file:
You can now “commit” (i.e. create a snapshot of the file in its current state to store on Git) any changes you make to this file simply by typing “commit Readme” into Terminal.
As you can see, installing and working with Git on Ubuntu is quite easy once you get a hang of the basic commands. Make sure that you spend enough time with basic Git commands; you will use them extensively in your career as a programmer. This course on Git basics is a great place to start.