What is Git?

Git is the world’s most popular version control software. But that raises another question; what is version control?

Version control is a system of saving changes to files in steps. In Git, these steps are called ‘commits.’ When you use version control, all changes to your codebase are kept and are organized for you. This is extremely useful if you want to do something like roll back a change, or pinpoint when a bug was introduced.

Git 101

Repositories

A project controlled by git is called a ‘repository’, or ‘repo’ for short.

Repositories can be created locally, but they can also be uploaded to Git servers like GitLab or GitHub. Those repositories can then be ‘cloned,’ or downloaded, from the server to a local device. This allows many people to work on the same project, then push their changes back to the server when complete.

Commits

In Git, changes to code are called commits. In addition to the code changed, a commit has a message, an author (a name and email), a timestamp, and information necessary to track the commit.

One useful piece of information is a commit’s hash. The hash is a unique identifier for the commit. It’s a SHA-1 hash, which is pretty long (4ba3802f4c30440e7b12d9bf18eda8c577e23742). To make things easier, Git will accept as few characters as possible while staying unique. For example, 4ba380 would be a valid shorthand for this hash. But if there were two commits starting with 4ba380, then it wouldn’t be unique, so you’d need to use a few more characters.

Here’s a common representation of a commit:

commit 4ba3802f4c30440e7b12d9bf18eda8c577e23742
Author: Kian Kasad <kian@kasad.com>
Date:   Fri Aug 21 22:27:34 2020 -0700

    fix copyright statement

You can see the hash in the first line, then the author, the date, and the commit message. The message makes it easy to tell what was changed without having to look into the code.

Branches

Another important feature of Git is branching. You can think of the history of a Git repository like a tree. Commits are placed on branches. Most repositories only have one main ‘master’ branch, but it can be useful to have multiple.

Unless otherwise specified, assume Git trees start from the bottom, i.e. the bottom commit is the oldest, and the top is the newest.

For example, take a look at the following tree:

│
├──── commit 1b82590d5963681fd7067c1438b2b6a62a9fb6b4 (master)
│\    Merge: bef9f4f bf205a2
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:20:17 2020 -0700
│ │ 
│ │       Merge branch 'feature-bar' into master
│ │ 
│ ├── commit bf205a2861df24f03d9c26b42009e232d792b78e (feature-baz)
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:19:57 2020 -0700
│ │ 
│ │       complete feature 'bar'
│ │ 
│ ├── commit 992fd2db772001d46cb733beb37d592f4b70282a
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:19:10 2020 -0700
│ │ 
│ │       continue work on feature 'bar'
│ │ 
├──── commit bef9f4f07c11e6fe7480a9fff74f1cafc45303e2
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:19:32 2020 -0700
│ │ 
│ │       fix bug 'baz'
│ │ 
│ ├── commit 6891ce95a2352ab4c16e2112d55753f73c301f78
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:18:51 2020 -0700
│ │
│ │       start work on feature 'bar'
│/
│
│ 
├── commit 213b0fbfdb532616313d3c242a69daa131139ed6
│   Author: Kian Kasad <kian@kasad.com>
│   Date:   Sun Aug 23 00:18:15 2020 -0700
│ 
│       fix bug 'foo'
│ 
├── commit f40d8f3e8beaa71391fe4040a4d695b41c5b4197
│   Author: Kian Kasad <kian@kasad.com>
│   Date:   Sun Aug 23 00:17:14 2020 -0700
│ 
│       initial commit

As you can see, a separate branch, feature-baz, was created to work on feature baz. Work on the master branch progressed normally (there was a bugfix), while feature baz was developed simultaneously. Once feature baz was finished, it was ‘merged’ into the master branch.

Branching provides an easy way to integrate multiple features or changes in a single repository.

Tags

Git also has a feature called ’tags.’ Tags are really just named pointers to specific commits.

They’re useful for specifying versions of a program. When a new version of a program is released, it may be tagged in the Git tree.

Here’s the same tree from earlier, but now with tags:

│
├──── commit 1b82590d5963681fd7067c1438b2b6a62a9fb6b4 (master) (tag: v1.2.0)
│\    Merge: bef9f4f bf205a2
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:20:17 2020 -0700
│ │ 
│ │       Merge branch 'feature-bar' into master
│ │ 
│ ├── commit bf205a2861df24f03d9c26b42009e232d792b78e (feature-baz)
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:19:57 2020 -0700
│ │ 
│ │       complete feature 'bar'
│ │ 
│ ├── commit 992fd2db772001d46cb733beb37d592f4b70282a
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:19:10 2020 -0700
│ │ 
│ │       continue work on feature 'bar'
│ │ 
├──── commit bef9f4f07c11e6fe7480a9fff74f1cafc45303e2
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:19:32 2020 -0700
│ │ 
│ │       fix bug 'baz'
│ │ 
│ ├── commit 6891ce95a2352ab4c16e2112d55753f73c301f78
│ │   Author: Kian Kasad <kian@kasad.com>
│ │   Date:   Sun Aug 23 00:18:51 2020 -0700
│ │
│ │       start work on feature 'bar'
│/
│
│ 
├── commit 213b0fbfdb532616313d3c242a69daa131139ed6 (tag: v1.1.4)
│   Author: Kian Kasad <kian@kasad.com>
│   Date:   Sun Aug 23 00:18:15 2020 -0700
│ 
│       fix bug 'foo'
│ 
├── commit f40d8f3e8beaa71391fe4040a4d695b41c5b4197
│   Author: Kian Kasad <kian@kasad.com>
│   Date:   Sun Aug 23 00:17:14 2020 -0700
│ 
│       initial commit

These tags could be used to jump to specific versions of the program/project. In reality, Git is just going to the commit pointed to by the tag, but being able to name/version commits makes some tasks much easier.

What’s next?

The next tutorial will go over the basic Git commands to create and work on a repo. Git can be very complicated, so I’ll only introduce a few features each time.