Course contents · Lesson 8 of 17

What Is Git? Version Control Explained Simply

Git and Collaboration · Lesson 8 of 17 · 11 min Engineer

You change a page in your project, and now nothing works. Yesterday's version was fine, but you saved over it, and files named final, final-2 and final-really don't say which one worked. Software teams face this every day. This lesson answers what is Git, the tool teams use to keep every version of their code, and teaches the words every later Git lesson uses.

What is Git? Snapshots of your project

Version control is a system that records changes to files over time, so you can see who changed what and bring back an earlier version. Git is a version control tool: a program on your computer that you usually drive by typing commands in a terminal.

When your work is worth keeping, you make a commit: Git records a snapshot, a full picture of every file it tracks at that moment. Files that did not change are not stored again; the snapshot links to the copy Git already has, so commits stay small.

Commits, one after another, form the project's history. Git names each commit with a hash, a 40-character ID of digits and the letters a to f, calculated from the commit's contents. Most screens show only the first seven or so characters, such as c92b5a0.

The timeline shows three commits. Notice that the arrows point backwards: each commit records its parent, the commit just before it, so adding a new save never changes an old one.

Timeline of three commits from left to right. Commit A, illustrative ID 3f1c2ab, Add home page; commit B, 8d40e17, Fix typo in title; commit C, c92b5a0, Add contact page, highlighted as the current one. Each card says snapshot of every file. Arrows labelled parent run from C back to B and from B back to A. Above C, HEAD points to the branch name main, which points to C, marked you are here. A banner says go back any time; later commits stay saved
Each commit points back to its parent, and HEAD marks the save point you are on.

HEAD is Git's marker for where you are. It normally points to a branch, a movable name for a line of work such as main, and the branch points to its newest commit, moving forward with each new one. Branches get their own lesson later.

Going back is safe: Git can show any old commit, compare two commits line by line or bring back an earlier version of a file, and none of that deletes later commits.

Local and free: Git, GitHub and GitLab

Git is free and open source, under the GNU General Public License version 2, and runs on Windows, macOS and Linux. It works on your own machine: committing or reading the history needs no network, because the whole history sits on your disk.

GitHub and GitLab are websites that host shared copies of Git projects and add team tools, such as pull requests for reviewing changes. Git is the tool; GitHub is one place to keep a copy, and you can use Git without either.

Four key words: repository, working folder, staging area, commit

A repository, or repo, is a project that Git tracks. Its history lives in a hidden folder named .git at the top of the project, which git init creates.

The working folder, which Git's documentation calls the working tree, holds the ordinary files you see and edit. Changes there are not part of the history yet.

The staging area, also called the index, holds the next snapshot while you build it. git add copies a file's current content into it. Edit that file again afterwards and you must run git add again, or the commit gets the older version.

git commit saves what is in the staging area as a new commit in the repository. Staging first lets you choose what goes in, so two unrelated fixes can become two clear commits.

The flowchart follows one change through the three areas. Spot the file that was edited but never added: it stays behind in the working folder.

Flowchart in three steps. 1, Working folder, the files you edit, not in history yet: contact.html is new and notes.txt is edited. An arrow labelled git add leads to 2, Staging area, the next snapshot in progress, also called the index: contact.html is staged and the notes.txt edit is not. An arrow labelled git commit leads to 3, Repository, .git, every commit kept: new commit C, Add contact page, on top of B, Fix typo in title, and A, Add home page. A banner says unstaged edits, like notes.txt, stay out of the commit
Only what you stage with git add goes into the next commit.

What a commit holds

A commit is a small record. It holds a pointer to the snapshot, its parent's hash, your message, and an author and committer, each with name, email and time. The author wrote the change; the committer saved it as this commit. The first commit has no parent, and a commit that joins lines of work has two or more.

You can print the record of the commit you are on with git cat-file -p HEAD (-p means pretty-print). This is the second commit from the Try it below, with example hashes and time:

tree 2e81171448eb9f2ee3821e3d447aa6b2fe3ddba1
parent 3f1c2ab9e04d6b7c5a8f1e2d3c4b5a6978695a4b
author a <a@x.io> 1796200000 +0000
committer a <a@x.io> 1796200000 +0000

second

tree is the snapshot and parent the previous commit. The number after each email is the time in seconds since 1 January 1970, then the time zone. Author and committer are usually the same person. Last comes the message.

The hash is calculated from all of this, including the parent's hash. Change anything and you get a new commit with a new hash, and every later commit's hash changes too. That is how Git spots damaged history.

Why teams need Git

Without version control, two people editing one project on a shared drive overwrite each other: whoever saves last wins, silently. With Git, each person works in a clone, a full copy of the repository with all its history, and commits there.

Teams share work through one agreed copy on a server, called the remote, often hosted on GitHub or GitLab. People send their commits to it and fetch everyone else's; the next lesson does this with git push and git pull.

A normal push never overwrites a teammate's commits: if someone sent commits first, Git makes you bring theirs in before yours go up. If you both changed the same lines, Git stops and asks a person to choose, a merge conflict that a later lesson teaches you to resolve.

History also shows which commit changed a line, who made it and why, so you can undo just that commit. In the diagram, notice commit D: it exists only on Asha's laptop until she sends it, because committing and sharing are separate steps.

At the top, a shared copy on a server, such as GitHub or GitLab, holding commits A, B and C. Below, Asha's, Ravi's and Meera's laptops, each a full clone with all history, commits A, B and C. Asha's laptop also has a new commit D, marked only here. Two-way arrows join each laptop to the shared copy, labelled send and fetch commits. A banner says every clone holds the full history
Every clone holds the full history, and a new commit stays local until you send it.

Check yourself

You edit notes.txt, run git add notes.txt, edit it again, then commit. Which version is in the commit?

The version from when you ran git add, because add copies the content at that moment into the staging area and commit saves the staging area. Run git add again to include the second edit.

GitHub is down for an hour. Can you still commit and read the project's history?

Yes. Your clone holds the whole history and Git works locally, so committing and reading history need no server. You need GitHub only to share commits, and you can send them when it is back.

Someone changes the message of an old commit. Why does every later commit get a new hash too?

Each hash is calculated from the commit's contents, which include its parent's hash. The old commit's new hash changes its child's parent line, so the child's hash changes, and so on down the line.

Go deeper