Getting started with Git on Ubuntu

Whether or not you use version control, I think you'd agree that it's important to track your changes and keep thorough records of your work on a project. Your future self will thank you whenever you need access to a previous state of it. Trust me, this happens more often than you think.

You can do it manually but it gets unmanageable very quickly. Unless you're one of those persons who needs to experience pain before seeing the light then I'd suggest you listen to my advice and start using version control from the very start.

These days there are many great free and open source tools to choose from:

Here's a great article on the advantages and disadvantages of centralized vs. distributed version control systems.

And here's the current trend among these tools.

I use Git at work and for my personal projects so that's what I'm going to show you how to install, configure and use in this post.

Let's get started.

Install

N.B. The instructions that follow were verified on a 64-bit Ubuntu 14.04 LTS system. You can find the latest Ubuntu release here.

The standard way of installing Git on Ubuntu doesn't give us the latest version. We can check this with apt-cache.

$ sudo apt-get update
$ apt-cache policy git
git:  
  Installed: (none)
  Candidate: 1:1.9.1-1ubuntu0.3
...

Hence, to ensure we get the latest version we will use the PPA provided by the "Ubuntu Git Maintainers" team.

$ sudo add-apt-repository ppa:git-core/ppa
$ sudo apt-get update

Now, if we check the version of Git that will be installed we see that it is the latest version.

$ apt-cache policy git
git:  
  Installed: (none)
  Candidate: 1:2.10.2-3~ppa1~ubuntu14.04.1
...

This is what we want, so let's install it.

$ sudo apt-get install git

Let's check that it was installed correctly.

$ git --version
git version 2.10.2  

And, that's it.

Configure

Some basic configuration is necessary to get started using Git.

The first thing you need to do is set your full name and email address. This information will be recorded in the commits you make.

$ git config --global user.name "Dwayne Crooks"
$ git config --global user.email "dwayne@simplydjango.com"

Next you should set a default text editor that you'd like Git to use when it needs extra input from you, for e.g. to add a commit message or to configure an interactive rebase.

$ git config --global core.editor "atom -nw"
# or
$ git config --global core.editor "nano -iT 2"

Aliases are not necessary when you're now starting out. But, as you continue to use Git on a daily basis you'd find yourself using a couple commands over and over again. Aliases can help you to reduce the amount of typing you need to do to achieve the same results and it frees your mind from having to remember the details of complex commands. Here are a couple aliases to get you started:

$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.co checkout
$ git config --global alias.st status
$ git config --global alias.hist "log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short"

As an example, here's how you'd use the hist alias. Instead of typing:

$ git log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short

You can just type:

$ git hist

And get the same results.

Finally, we can tell Git about all the files we never ever want it to track. It's recommended that you only put system specific files that shouldn't be allowed to sneak into your repositories or your .gitignores.

A good start for a Linux based machine is given here. And, check here for a full list of globally useful gitignores.

$ wget https://raw.githubusercontent.com/github/gitignore/master/Global/Linux.gitignore -O ~/.gitignore_global
$ git config --global core.excludesFile "$HOME/.gitignore_global"

Use

Here's a simple example to illustrate how you'd use Git.

Create a directory and add a Python module to it.

$ cd /tmp && mkdir hello-world && cd hello-world
$ echo "print('Hello, world')" > hello.py

# Test that it works
$ python hello.py
Hello, world  

Initialize a Git repository and start tracking the file.

$ git init
$ git add hello.py
$ git ci -m "My first Python program"

$ git log
commit fedce34e7fe53a582e47741810bbce90110de377  
Author: Dwayne Crooks <dwayne@simplydjango.com>  
Date:   Fri Nov 18 08:33:39 2016 +0000

    My first Python program

$ git hist
* fedce34 2016-11-18 | My first Python program (HEAD -> master) [Dwayne Crooks]

Great! You're set to begin using version control in your future projects.

To learn more, I'd recommend you read Pro Git.

Bonus

Click here to spin up a virtual machine and play with the latest Git.

Resources

P.S. Here's my challenge to you. Install Git or any other version control system and start using it in your current or next project. Are you already using Git? Feel free to share your experiences in the comments below.