What Is Git

Updated · 12 min read
easyGit 2.53gitbasicsrepositorycommitinterview

Navika, Srihitha and Advitha are building a small Java app called Fee Tracker, which records school fee payments. Three people will change the same files for months. They need to know what changed, who changed it and why, and they need a way back when something breaks.

The tool they use is Git. It is the version control system used by almost every software team in the world, from two students on a college project to the thousands of developers who build the Linux kernel.

In this lesson, you will learn what Git is, where it came from, and the four ideas that everything else in Git is built on. You will also watch a real Git session from start to finish.

Git Is a Distributed Version Control System

Git is a free, open-source distributed version control system. It records the history of a project as a series of save points, and it gives every person a full copy of that history on their own computer.

Git is a program that you install and run on your computer. You talk to it by typing commands that start with the word git, such as git status or git commit. Editors such as VS Code and IntelliJ IDEA have buttons for the same commands, but underneath they run the same Git.

Git isWhat that means for you
DistributedThe whole history is on your computer, so you can work without a network
FastAlmost every command reads local files and finishes in a moment
SafeEvery version gets an ID calculated from its content, so a damaged or altered file is noticed
Good at branchingYou can start a separate line of work in a second and combine it later
Free and open sourceNo license fee, no account, and anyone can read its source code
EverywhereNearly every company, open-source project and coding interview expects it

A Short History of Git

In 2005, the developers of the Linux kernel lost access to the version control tool they had been using. It was a commercial product called BitKeeper, and its free license for open-source projects was withdrawn.

Linus Torvalds, the creator of Linux, looked at the other tools available. None of them was fast enough for a project with thousands of contributors, so he wrote his own. He started in April 2005, and within the same month Git was storing its own source code and the Linux kernel's. He wanted three things from it: speed, a design where every developer has the full history, and strong protection against damaged or altered data.

A few months later, Linus handed the project to Junio Hamano, who has led Git's development ever since. The name is a joke. "Git" is British slang for an unpleasant person, and Linus said he names his projects after himself.

Today Git is the standard. In Stack Overflow's 2022 developer survey, about 94 percent of developers said they use it. This course uses Git 2.53.

The Key Ideas in One Picture

Four words describe most of what Git does. Learn these four and the rest of the course has something to hang on.

Repository, commit, branch and remote
  1. A repository (often shortened to repo) is your project folder together with its whole history. Git keeps the history in a hidden folder named .git inside the project.
  2. A commit is one save point: a snapshot of all your files at one moment, with an author, a date and a message. Each commit remembers the commit that came before it, so together they form a chain. That chain is the project's history.
  3. A branch is a label that points to a commit. The default branch is usually named main. When you make a new commit, the label moves forward to it.
  4. A remote is another copy of the repository, usually on a hosting service such as GitHub. You push to send your commits to it and pull to bring other people's commits from it. The first remote is usually named origin.

A First Look at Git

Here is Navika starting the Fee Tracker project. You don't have to type anything yet. Installing Git is the next step in the course, and every command below gets its own lesson. For now, read the commands and the output, and notice how little is needed.

First, she creates a folder for the project and asks Git to start tracking it:

Terminal
mkdir fee-tracker
cd fee-tracker
git init
Output
Initialized empty Git repository in /home/navika/projects/fee-tracker/.git/

git init turns the folder into a repository by creating the hidden .git folder. The history is empty so far.

Next, she creates a file and saves the first version of the project:

Terminal
echo "Fee Tracker" > README.md
git add README.md
git commit -m "Add project README"
Output
[main (root-commit) d334daf] Add project README
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

How it works

  1. echo "Fee Tracker" > README.md creates a file named README.md that contains one line of text.
  2. git add README.md tells Git that this file should go into the next commit.
  3. git commit -m "Add project README" saves the commit. The text after -m is the message that explains the change.
  4. Git answers with the branch (main), the commit's short ID (d334daf), the message, and a summary of what changed.

She adds a second line to the file and commits again:

Terminal
echo "Tracks school fee payments." >> README.md
git add README.md
git commit -m "Describe what the app does"
Output
[main eebb6b8] Describe what the app does
 1 file changed, 1 insertion(+)

Now the project has a history, and git log shows it:

Terminal
git log --oneline
Output
eebb6b8 (HEAD -> main) Describe what the app does
d334daf Add project README

The newest commit is at the top. (HEAD -> main) marks where you are now: on the branch main, at its newest commit. Navika can return to either version of the project at any time, and she did all of this without a network or an account.

Git Words You Will Meet

You will see these words in every Git lesson, article and error message. Each one is explained fully later. This table is a preview, so that none of them is a stranger.

WordMeaning
RepositoryA project and its full history, stored in the .git folder
Working treeThe project files you see and edit in your folder
Staging area (or index)The list of changes that will go into the next commit; git add puts changes there
CommitA saved snapshot of the project, with an author, a date and a message
Commit ID (or hash)The unique name of a commit, such as eebb6b8
BranchA movable label that points to a commit; main is the usual default
HEADGit's name for "where you are now": the commit your files currently match
RemoteAnother copy of the repository, usually on a hosting service
CloneTo download a full copy of a repository, history included
Push and pullTo send your commits to a remote, and to bring commits from it
MergeTo combine the work from two branches

Common Mistakes

Thinking Git and GitHub are the same thing. Git is the tool on your computer. GitHub is a website that stores copies of Git repositories. You can use Git for years without ever opening GitHub. The next lesson compares them.

Expecting Git to save your work automatically. Git is not an autosave. Nothing is recorded until you run git commit. That is on purpose: you choose which changes belong together and you describe them.

Believing a commit uploads your code. A commit is saved only in the repository on your computer. Nothing leaves your machine until you push. Many beginners commit, close the laptop, and are surprised that teammates can't see the work.

Being afraid to break something. Git is built to keep what you commit. Once work is in a commit, it is very hard to lose by accident, and this course shows you how to recover from each kind of mistake. Commit often and experiment freely.

Interview Corner

What is Git?

Git is a free, open-source distributed version control system. It records the history of a project as a series of commits, where each commit is a snapshot of the files with an author, a date and a message. Every developer has a full copy of the repository, so most operations are local and fast, and work can continue offline.

Who created Git, and why?

Linus Torvalds created Git in 2005 for the development of the Linux kernel, after the free license for BitKeeper, the tool the kernel developers were using, was withdrawn. His goals were speed, a fully distributed design, strong support for thousands of parallel branches, and protection against corrupted data. Junio Hamano has maintained Git since 2005.

What is a repository?

A repository is a project together with its complete history. In Git, the history is stored in the hidden .git folder at the top of the project. A repository on your own computer is a local repository, and a copy on a server such as GitHub is a remote repository.

What is a commit?

A commit is a snapshot of the whole project at one moment, saved with the author's name, the date and a message. Each commit has a unique ID calculated from its content, and each one points to the commit before it, which is how the history is linked together.

Why is Git called distributed?

Because there is no single copy that everyone depends on. Each clone is a full repository with the entire history. Developers commit locally and then exchange commits with other repositories by pushing and pulling. If a server is lost, any developer's copy can restore it.

Practice

Challenge: add a third commit. Continue the session above. Add the line Built by Navika, Srihitha and Advitha. to README.md, commit it with the message Add the team names, and show the history with git log --oneline.

If Git is already on your computer, try it now. If not, read the solution and come back after the lesson "How to Install Git".

What does Git print? Before you open the solution, decide how many lines git log --oneline will show, and which commit will be at the top.

Show solution
Terminal
echo "Built by Navika, Srihitha and Advitha." >> README.md
git add README.md
git commit -m "Add the team names"
git log --oneline
Output
[main b224235] Add the team names
 1 file changed, 1 insertion(+)
b224235 (HEAD -> main) Add the team names
eebb6b8 Describe what the app does
d334daf Add project README

The first two lines come from git commit. The last three come from git log: three commits, with the newest at the top, and main now pointing to it. These three commits are the ones in the diagram earlier in this lesson.

Quick check: What is Git

4 questions
1Where does Git keep the history of a project?
2What is a commit?
3You run git commit on your laptop. Who can see the new commit?
4What does main usually refer to?

Key Takeaways

Quick reference

CommandIt is used to
git initTurn the current folder into a Git repository
git add <file>Choose a file's changes for the next commit
git commit -m "message"Save a commit with a message
git log --onelineShow the history, one line for each commit

Was this tutorial helpful?

Lesson 2 of 106