This post offers a concise introduction to the fundamental concepts of Git, briefly covering its basic principles.In another related post I presente a practical, hands-on introduction to Git, so you can experiment and try the fundamentals of Git.

What is it?

The standard definition: Git is a distributed version control system.

And what is a distributed version control system? First of all, it is a version control system, or VCS. VCSs are systems that allow to keep the history of our work, when we are developing code.

Version Control Systems: why do we need them?

Everyone that develops code has had this experience:

  • 🟢 I have code that works;
  • 🟡 I “improve” the code
  • 🔴 I now have code that does not work. 🫏

What do we do now? What have we changed? Do we remember everything we did? When this happens, it would be good to go back to the last point where code was working. That’s what VCSs do: by keeping a record with the history of our code, they allow to “travel back in time” and return to the point we were before “improving” our work to a messy amount of code lines generating errors or creating programs that crash shamefully.

How do VCSs work

A VCS keeps snapshots (“photographs”) of your code (or any set of text files1) at given points in time. The place where it keeps those snapshots is the repository (or repo, for short).

In most systems (if not in all) the command for taking a picture of your code is called commit: when you issue a commit command, the VCS looks at your code and creates an image in the repository. This image is assigned a number; it can be a sequential number (1, 2, 3,…) or the system can generate a random, large, unique number that is associated to the commit. In Git, these numbers are a 40 character long string, calculated from the contents of the file or directory structure, and they are generated by a SHA-1 hash.

Identifying a commit by the sequential number or by the hash is not always practical, an so Git introduces mechanisms like the tags (you can find more information about tags here)

You can continue your work and, every time you feel you have a good result, you can issue a new commit: a new version of your code is stored in the repository. In this way, you will be constructing an history of your code development, with specific moments in time (the “snapshots” or commits) stored in the repository.

If, at some time, you find that you need to recover an older version of your code, you only need to perform the reciprocal of the commit, which is a checkout: when you checkout one of your older versions, the code in your working repository is replaced by the code of that commit.

So, by using commit and checkout, you can travel back and forth in time, and getting older versions of your code or going back (or forth) to the most recent. There are many more commands to use with VCSs, but commit and checkout will be the two most important ones, for a start.

And what about distributed version control systems?

Distributed version control systems (DVCS) are version control systems where the repository is not in a single location, but it can exist concurrently in several computers. Git is perhaps the most common DVCS used today.

Installing Git

All install files and instructions can be found in the Downloads page of Git web site. There are versions for Linux, Windows and MacOS operating systems.

Linux

Git is distributed as a standard package in most distributions. In Debian-based distros, such as Ubuntu, you only need to issue the command:

$ sudo apt install git

If you are on a Red-Hat based distro, like Fedora:

$ sudo dnf install git

Alternatively, you may want to install git-all. git-all is a dummy package that, by installing, causes the installation of the several packages related to Git.

After installing, you can test the installation by running:

$ git --version

Windows

The installation file is available in https://git-scm.com/download/win. If you access Git site under Windows, you should see an image with the link to the Windows version. The installation is standard: just download the installer and run it.

I have installed Git. How do I (learn to) use (G)it?

This post contains a hands-on introduction to Git, where you can exercise the basic Git commands.

The canonical source to learn how to use Git is Git’s documentation page, where you can find the Reference Manual, videos on Git, and a curated list of external references.

More about Git and version control systems (distributed or not)

Wikipedia contains a fairly good explanation about version control and distributed version control.

Another fundamental reference is The Git Book. You can read the book online or buy a dead tree version on Amazon.

  1. Although VCS can work with binary files, they are not efficient in dealing with them, and keeping binary files in a repository should be avoided whenever possible .