Loading

T7 Git and GitHub · Lesson 1

Why version control

Keeping one file and its whole history instead of a pile of copies: what version control is, a repository and its .git folder, the three places a change passes through, commits with who, when and why, reading the history, and telling git who you are.

  • say what version control gives that a pile of copies cannot
  • make a repository with git init, and read what git status says
  • name the three places a change passes through: the working tree, the staging area and the repository
  • record a commit with git add and git commit, and write a message that says why
  • read the history with git log, and know what a commit's name is
  • tell git your name and email on a real machine
About 45 minutesChecked against Git on the laptop (the kit compares every git block with it, in its sandbox) 2.53.0 (2026-11-07); GNU Bash on the laptop (the kit compares every shell block with it, in a sandbox) 5.3.9 (2026-11-07)Play this lesson's games

The back cover

Everyone who writes something long ends up with a folder of copies named final, final2 and final-really. Version control keeps one file instead, and remembers every version you chose to keep, with who made it, when and why.

These pages show what version control is, and how git turns a folder into a repository. They show the three places a change passes through, and how to file a commit with a message that says why. They also show how to read the history, and how to tell git who you are.

Before you start

The story: Copies of copies

Warden Rao's lantern lights the way up a long stair to the Archive, a hall of drawers that climbs into the dark. On the first table lie the sluice orders, in nineteen copies: orders, orders new, orders final, orders final two, orders really final. Corin needs last Tuesday's version, and nobody can say which copy it is.

Two of the copies even disagree about which gate opens at noon.

Keeper Yusra Hollis sweeps the nineteen sheets into a single pile. "Keep one sheet," she says, "and keep its whole life in the drawer beneath it. Write down why. The what will speak for itself."

Every developer keeps their work in git. It remembers every version of every file, says who changed what and why, and lets many people work on one project without overwriting each other. This step teaches it from the first command, and this first lesson answers the question underneath: why keep versions at all, and what does git actually keep?

It builds on these ideas. Each one has a tiny example, so you can check that you remember it.

What you already knowA tiny exampleWhere it comes back
A terminal and a prompt.pwdGit is used from the terminal.
Folders and files.ls -aA repository is a folder.
Hidden names..bashrcGit keeps its records in .git.
The exit status.echo $?Git's commands report success the same way.
Reading an error.fatal: ...Git's errors start with fatal.
cat and echo with >>.echo "x" >> fileTo change a file between versions.
Copies.cp a.txt a-old.txtWhat git replaces.
Environment and settings.~/.bashrcGit has a settings file of its own.
Permissions on folders.ls -ldGit records file modes too.
Scripts../round.shScripts are files worth keeping versions of.

By the end of this lesson you will be able to:

  • say what version control gives that a pile of copies cannot;
  • make a repository with git init, and read what git status says;
  • name the three places a change passes through: the working tree, the staging area and the repository;
  • record a commit with git add and git commit, and write a message that says why;
  • read the history with git log, and know what a commit's name is;
  • tell git your name and email on a real machine.

How to follow this lesson

The blocks run in the small shell, which has a small git of its own. Each block was also run in real git, with the same name, email and clock, and both printed exactly what you see, down to the commit names. On this page the clock starts at 10:00 on 1 January 2026 and moves one second for each git command, which is why the times in the history look so tidy.

In one line

Git keeps one copy of your work and its whole history; this lesson shows why that beats a pile of copies, and how to start.

Why keep versions at all

At a glance

Everyone who has written anything long has a folder like this: a pile of copies with hopeful names, each saved when the work seemed finished.

A pile of copies loses four things: when each copy was made, who made it, why, and what changed. A version control system keeps one file and records every version you choose to keep, with who, when and why. Git is the one nearly every developer uses, and it is distributed: every copy of a project holds its whole history.

Take a guess: Why was git written, and by whom?

Everyone who has written anything long has a folder like this one. Each time the work seemed finished, someone saved a copy with a hopeful name:

On the Archive's table: the orders and three copies of them

ls
orders-final-2.txt  orders-final.txt  orders-new.txt  orders.txt

Now try to answer some simple questions. Which copy is the latest? What changed between final and final 2, and who changed it, and why? What did the orders say last Tuesday? The names cannot tell you. The answers were never written down anywhere.

A version control system answers all of them. You keep one file, under its one name, and the system keeps every version of it that you chose to record, each with who made it, when, and a note saying why.

Version control

A way of recording the changes to a set of files over time, so that any earlier version can be seen or brought back, with who changed what, when, and why.

You wantWith copiesWith version control
the latest versionguess from the namesthe file itself
what changedcompare two files by eyeask for the difference
who and whynobody wrote it downrecorded with every version
last Tuesday's texthope a copy was made thenask for it by date or name
to undo a mistakefind a good copy, if anygo back to any recorded version
two people at onceone overwrites the otherboth changes kept, and joined

Git

The version control system that nearly every developer uses is git. Its own manual calls it a fast, scalable, distributed revision control system; revision is another word for version. Distributed means that every copy of a project carries the whole history, not only the latest files, which the remotes lesson explains.

Git works on any kind of file, but it is at its best with text: code, notes, settings, scripts. It can show you exactly which lines of a text changed.

Worked example 1

Warden Rao asks what the copies on the Archive's table cannot tell her, and what a version control system would record instead.

What we have: four copies of the orders with hopeful names

What we need: the missing facts, and what keeps them

The plan: list the questions the names cannot answer; for each, the fact a recorded version carries

  1. Which is newest: a name such as final 2 says nothing about time. A recorded version carries its time.
  2. Who changed it and why: no copy records a person or a reason. A recorded version carries both.
  3. What changed: only a slow comparison by eye. Version control can show the changed lines.

On the Archive's table: the orders and three copies of them

ls
orders-final-2.txt  orders-final.txt  orders-new.txt  orders.txt
  1. Four names, and not one fact about time, person or reason.

The copies lack time, person, reason and difference; version control records all four.

Check: each row of the table above gives one of those four.

A path that fails: adding the date to every copy's name. The names get longer, but they still say nothing about who or why, and the folder still grows with each save.

When you are stuck: version control seems like extra work

Count the time spent looking for the right copy, and the times a good version was lost.

You record a version only when the work reaches a point worth keeping, not on every keystroke.

The history is kept for you, out of the way, in one hidden folder.

Every developer's tools assume it: the next steps of this school use it daily.

Your turn

Name two questions about a file that a pile of copies cannot answer.Tap to reveal
  1. Think about time and people.

For example: who changed it, and why.

What does distributed mean for git?Tap to reveal
  1. Every copy of a project carries the whole history.

Every copy holds the whole history, not only the latest files.

Is git only for code?Tap to reveal
  1. It works on any file, at its best on text.

No: any file, and best of all text.

Did you know?

Git began in 2005

Git was written for Linux. The Pro Git book explains that from 2002 the Linux kernel used BitKeeper, a proprietary tool owned by a company, and that in 2005 its free-of-charge status was revoked. The Linux community, Linus Torvalds above all, then built a tool of its own. Its goals included speed, a simple design, thousands of parallel branches and being fully distributed.

Why it fits here: It answers this topic's question: the Linux community, led by Linus Torvalds, wrote git in 2005 after BitKeeper stopped being free for them.

Sources: Pro Git: A Short History of Git; Wikipedia: Git

93.87% use Git

The claim that nearly every developer uses git has a number behind it. In Stack Overflow's survey of 2022, the first to ask about version control, 93.87% of the 71,379 people who answered named Git. Among professional developers the share was 96.65%. The next answer, SVN, was named by 5.18%, and 4.31% used no version control at all.

Why it fits here: It backs this topic's claim with a number: in 2022, 93.87% of Stack Overflow's respondents used Git.

Source: Stack Overflow Developer Survey 2022

In one line

A pile of copies loses time, person, reason and difference. Version control keeps one file and records every chosen version with who, when and why. Git, the system nearly every developer uses, is distributed: every copy holds the whole history.

A repository: git init

At a glance

Git works on one folder at a time. A folder that git looks after is called a repository, or repo for short.

The command git init turns the current folder into a repository, and records nothing yet. Run it once, in the project's top folder. Git keeps everything it records in a hidden folder named .git, inside your project. The command git status says which branch you are on, and how your files compare with what git has recorded; a new file shows up as untracked.

Take a guess: What did git's own first README call it?

Git works on one folder at a time. A folder that git looks after is called a repository, or repo for short. Ask git about a folder before it is one, and it says so plainly:

git status
fatal: not a git repository (or any of the parent directories): .git

The message also tells you where git looked: in this folder, and in every folder above it, for a folder named .git. Git's errors start with fatal, and end the command with a status of 128.

Repository

A folder whose files git tracks, together with the hidden .git folder inside it, where git keeps every recorded version and its own records. Everything git knows about the project is in .git.

git init turns the current folder into a repository. It makes the hidden .git folder, and that is all it changes:

git init
ls -a
ls .git
cat .git/HEAD
Initialized empty Git repository in /home/asha/shop/.git/
.  ..  .git
HEAD  config  description  hooks  info	objects  refs
ref: refs/heads/main

Look inside without changing anything. objects is where the recorded versions will go, refs holds the names that point at them, and config holds the repository's own settings. HEAD is a small text file saying where you are: on the line of work named main, which the branches lesson explains. You will rarely open .git, and you should never edit it by hand.

Never remove .git

The .git folder is the whole history. Removing it leaves your current files exactly as they are, but every recorded version, every name and every note is gone.

git status: what git sees

The command you will type more than any other is git status. It says which line of work you are on, and how the files compare with what git has recorded. In a new repository there is nothing yet:

git init -q
git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

The -q, for quiet, only stops git init from printing its line. Read status from the top: the branch, then the fact that nothing has been recorded yet, then what you can do next. Git's messages often end with a hint in brackets, and the hints are worth reading.

Worked example 2

Start a repository for the sluice orders, in the folder that already holds orders.txt, and see what git makes of the file.

What we have: a folder with orders.txt and no repository

What we need: a repository, and git's view of the file

The plan: git init makes the repository here; git status reports on every file in it

In a folder that holds orders.txt, the sluice orders, with one line

git init
git status
Initialized empty Git repository in /home/asha/shop/.git/
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	orders.txt

nothing added to commit but untracked files present (use "git add" to track)
  1. orders.txt is untracked: git sees it, but has been told nothing about keeping it. Making a repository records nothing by itself.

The repository exists, and orders.txt is untracked.

Check: status names the file under Untracked files, and the hint says how to track it.

A path that fails: expecting git init to record the files that are already there. It only prepares the .git folder; recording is always a separate step that you choose.

When you are stuck: git says it is not a repository

Check where you are with pwd: git looks in this folder and the folders above it.

Run git init once, in the project's top folder, not in every folder.

A repository inside another repository is almost never what you want: check the folders above for .git with ls -a.

Read the hint at the end of git's message: it usually names the next command.

Your turn

git init -q
ls -a
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
.  ..  .git
  1. git init made one hidden folder.
  2. -a shows hidden names.

It prints . .. .git.

What does git init change in the folder?Tap to reveal
  1. It adds one hidden folder, and records nothing.

It adds .git, and nothing else.

What status does a git command end with when it prints fatal?Tap to reveal
  1. fatal ends the command with a fixed status.

128.

Did you know?

The stupid content tracker

Git's first README did not take its name seriously. Saved with the first version, in April 2005, it calls git the stupid content tracker and says the name can mean anything, depending on your mood. It might be a random three-letter word that no common Unix command used, or, when it works for you, global information tracker.

Why it fits here: It answers this topic's question: git's first README called it the stupid content tracker, or global information tracker on a good day.

Sources: git: the first README (commit e83c516); Wikipedia: Git

Everything in .git

Delete the .git folder, and that copy of the project forgets its whole history. The git init manual says it creates an empty repository, basically a .git directory with subfolders for objects, refs/heads and refs/tags. The layout manual says objects is the repository's object store, where its contents are kept, and that the file HEAD names the branch you are on.

Why it fits here: It shows what this topic's hidden .git folder holds: the objects, the branches and HEAD, which together are the repository.

Sources: git manual: git-init; git manual: gitrepository-layout

In one line

A repository is a folder that git tracks; its history and records live in the hidden .git folder. The command git init makes one, recording nothing yet. The command git status says which branch you are on and how your files compare with what git has recorded.

The three places a change passes through

At a glance

Think of an office that files photographs of documents. The papers you work on are on your desk, the photographs chosen to file next sit on a tray, and filed photographs go into a cabinet.

Git has the same three places. The working tree is where you edit your files. The staging area, also called the index, holds the next version, and git add puts a file there. The repository holds every version filed, and git commit files the tray's contents there. The command git status shows what is in each place.

Take a guess: Why does git status suggest git rm --cached, when the tray is called the staging area?

Think of an office that files photographs of documents. On your desk are the papers you are working on. On a filing tray beside you are the photographs you have chosen to file next. In the cabinet are the photographs already filed, each in its folder with a date. Git has the same three places, and every change passes through them in that order.

PlaceGit's nameHoldsMoved on by
the deskthe working treeyour files, as you edit themyou, in an editor
the filing traythe staging area, also called the indexthe next version, as it will be recordedgit add
the cabinetthe repository, in .gitevery recorded versiongit commit
Staging area

Git's record of exactly what the next commit will hold. git add copies a file's current contents into it; git commit records whatever it holds. Git's manuals also call it the index.

Here the orders move from the desk to the tray. git status shows the file under Changes to be committed, which means: on the tray, ready to be filed:

In a folder that holds orders.txt, the sluice orders, with one line

git init -q
git add orders.txt
git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   orders.txt

One file, two versions

The tray holds a copy of the file as it was when you added it, not a link to the file. Change the file again after git add, and git has two versions to tell you about. This block files a first version, then adds one change to the tray, makes a second change on the desk, and starts a new file of notes:

In a folder that holds orders.txt, the sluice orders, with one line

git init -q
git add orders.txt
git commit -q -m "First orders"
echo "close gate 2" >> orders.txt
printf 'pump 7 low\n' > notes.txt
git add orders.txt
echo "check gate 5" >> orders.txt
git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   orders.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   orders.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	notes.txt

Read the three sections as the three places. Changes to be committed: the tray holds a version of orders.txt with close gate 2. Changes not staged: the desk's orders.txt has changed again since, with check gate 5, and that change is not on the tray. Untracked: notes.txt is on the desk, and git has never been told about it. A commit now would file only what is on the tray.

status saysIt meansTo move it on
Untracked filesa file git has never been told aboutgit add file
Changes not staged for commitchanged on the desk since it was last addedgit add file
Changes to be committedon the tray, ready to filegit commit -m "why"
nothing to commit, working tree cleandesk, tray and cabinet agreenothing to do

Worked example 3

A clerk wants the next filed version of the orders to include close gate 2 but not check gate 5, which is only a trial. Show how the three places make that possible.

What we have: orders.txt with two new lines, one finished and one a trial

What we need: a tray holding only the finished line

The plan: add the file after the finished change, then make the trial change; the tray keeps the version from the moment of git add

In a folder that holds orders.txt, the sluice orders, with one line

git init -q
git add orders.txt
git commit -q -m "First orders"
echo "close gate 2" >> orders.txt
git add orders.txt
echo "check gate 5" >> orders.txt
git commit -q -m "Close gate 2"
git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   orders.txt

no changes added to commit (use "git add" and/or "git commit -a")
  1. The commit filed the tray's version, with close gate 2. The trial line was never added, so it is still only on the desk, and status shows orders.txt as changed and not staged.

The tray holds the orders with close gate 2 and without the trial.

Check: after the commit, status still shows the trial as a change not staged.

A path that fails: thinking that git add marks the file once and for all. It copies the file as it is at that moment; a later change needs another git add.

When you are stuck: status lists a file twice, or not at all

Listed twice: part of the change is on the tray and part only on the desk. git add it again to put the latest on the tray.

Not listed at all: it is the same in all three places, or it is in a folder git was told to ignore.

Untracked forever: git add it once; after that git follows it.

Unsure what will be filed: whatever is under Changes to be committed, and nothing else.

Your turn

Which command moves a change from the working tree to the staging area?Tap to reveal
  1. The tray is filled with one command.

git add.

A file appears under both Changes to be committed and Changes not staged. Why?Tap to reveal
  1. The tray holds the version from the last git add.
  2. The file was changed again afterwards.

It was changed again after git add.

git init -q
git add orders.txt
git status
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   orders.txt
  1. git add put orders.txt on the tray.
  2. Nothing has been committed yet.

It lists orders.txt as a new file under Changes to be committed.

Did you know?

The index was once the cache

The staging area has had three names. Git's glossary lists cache as an obsolete word for the index, and the old word lives on in commands. That is why git status suggests git rm --cached, which the manual says removes a path only from the index and leaves your file alone. The git diff manual adds that --staged is a synonym of --cached.

Why it fits here: It answers this topic's question: cache is git's old word for the index, which is why --cached still appears.

Sources: git manual: gitglossary; git manual: git-rm; git manual: git-diff

Three versions on one tray

The staging area usually holds one version of each file, but not always. Git's glossary says the index can also hold a second and even a third version, used when merging. The merge manual explains that for a file in conflict it records up to three: the version from the common ancestor, the one from your branch, and the one being merged in.

Why it fits here: It adds depth to this topic's staging area: during a merge it can hold three versions of one file.

Sources: git manual: gitglossary; git manual: git-merge

In one line

A change passes through three places: the working tree, where you edit; the staging area, or index, where git add puts the next version; and the repository, where git commit files it. git status shows what is where.

Commits: git add and git commit

At a glance

A commit is one filed version: a snapshot of everything on the tray, with your name, the time, and a message that says why.

First git add the files, then git commit -m with the message in quotes. The option -a stages changes to files git already tracks, so git commit -a -m does both steps for them; a new file still needs git add once. Git will not make an empty commit, and the message should say why, not only what.

Take a guess: How long should the first line of a commit message be?

A commit is one filed version: a snapshot of everything on the tray, with your name, the time, and a message saying why. git commit makes one. The option -m gives the message on the same line:

In a folder that holds orders.txt, the sluice orders, with one line

git init -q
git add orders.txt
git commit -m "First orders"
git status
[main (root-commit) 9c23f35] First orders
 1 file changed, 1 insertion(+)
 create mode 100644 orders.txt
On branch main
nothing to commit, working tree clean
Commit

A recorded snapshot of the files in the staging area, with the author, the time, a message, and a link to the commit before it. Each commit has a name that is unique to it, worked out from everything it holds.

Read git's report. main is the line of work, root-commit says that this is the first commit, and 9c23f35 is the start of the commit's name. Then come the subject and a count: one file changed, with one line added, and create mode, which says that the file is new and is an ordinary file, not a program. After it, status says that the desk, the tray and the cabinet agree.

Commit often, with -a for changed files

Once a file is tracked, git commit -a first puts every change to tracked files on the tray, then commits. It saves typing git add for each changed file. It does not add new files; those still need git add once:

In a folder that holds orders.txt, the sluice orders, with one line

git init -q
git add orders.txt
git commit -q -m "First orders"
echo "close gate 2" >> orders.txt
git commit -am "Close gate 2 for repairs"
[main f0ee597] Close gate 2 for repairs
 1 file changed, 1 insertion(+)

The -q on the first commit kept it quiet; the second reported. When there is nothing on the tray, git refuses to make an empty commit, shows the status instead, and ends with 1:

In a folder that holds orders.txt, the sluice orders, with one line

git init -q
git add orders.txt
git commit -q -m "First orders"
git commit -m "Again"
echo $?
On branch main
nothing to commit, working tree clean
1

A message that says why

The message is for people, and the most important person is you, a year from now. The change itself shows what changed; only the message can say why. Write a short first line, the subject, that reads as an order, such as Close gate 2 for repairs. If there is more to say, add a body after it: a second -m adds a second paragraph.

A weak messageA strong message
fixClose gate 2 for repairs
changesOpen gate 6 on Sundays for the market
update orders.txtMove gate 4's opening to noon

Worked example 4

File two versions of the orders: the first as it is now, and a second after closing gate 2. The second commit's message must give its reason as well: the gate's seal leaks, and the fitters need a week.

What we have: orders.txt, a change to make, and a reason to record

What we need: two commits, the second with a subject and a body

The plan: add and commit the first version; make the change, add it, and commit it with two -m options, one for the subject and one for the reason

In a folder that holds orders.txt, the sluice orders, with one line

git init -q
git add orders.txt
git commit -m "First orders"
echo "close gate 2" >> orders.txt
git add orders.txt
git commit -m "Close gate 2 for repairs" -m "The gate's seal leaks; the fitters need a week."
[main (root-commit) 9c23f35] First orders
 1 file changed, 1 insertion(+)
 create mode 100644 orders.txt
[main 0b33b9d] Close gate 2 for repairs
 1 file changed, 1 insertion(+)
  1. Git's report shows only the subject. The body is kept with the commit, and git log shows it, as the next section does.

Two commits: 9c23f35 First orders, and 0b33b9d Close gate 2 for repairs, with its reason.

Check: the second report has no root-commit, as it follows the first.

A path that fails: one commit for both changes. The history would then say that the orders began with gate 2 closed, and the reason for closing it would have no version of its own.

When you are stuck: git commit does not do what you expected

nothing to commit: nothing is on the tray. git add the files first, or use -a for tracked files.

A new file was left out: -a does not add new files; git add them once.

An editor opened: git wanted a message. Give one with -m next time; in the editor, write it, save and close.

The wrong thing was committed: the next lesson shows how to look before committing.

Your turn

What does root-commit mean in git's report?Tap to reveal
  1. It appears only once in a repository's life.

That this is the first commit, with none before it.

Does git commit -a include a file that git has never tracked?Tap to reveal
  1. -a stages changes to tracked files only.

No: a new file needs git add once.

Which part of a commit says why the change was made?Tap to reveal
  1. The change shows what; people need the reason too.

The message.

Did you know?

50 characters, then a blank line

Commit messages have a shape. The git commit manual says it is a good idea to begin with one short line of no more than 50 characters, then a blank line, then a fuller description. Git treats that first line as the commit's title. On 19 April 2008 Tim Pope wrote a note on commit messages, adding that the rest should be wrapped at about 72 characters.

Why it fits here: It answers this topic's question: a subject of at most 50 characters, a blank line, then a body wrapped at about 72.

Sources: git manual: git-commit; Tim Pope: A Note About Git Commit Messages

Write the subject as an order

The best subjects read like orders. The Linux kernel's guide to sending patches asks people to describe changes in imperative mood, as in make xyzzy do frotz, rather than makes xyzzy do frotz or I changed xyzzy. It says to write as if giving orders to the codebase to change its behaviour. Subjects such as Close gate 2 for repairs follow that rule.

Why it fits here: It sharpens this topic's message that says why: kernel developers write each subject as an order to the code.

Source: The Linux kernel documentation: Submitting patches

In one line

A commit files a snapshot of the staging area with your name, the time, and a message that says why. git add, then git commit -m "why"; git commit -a stages changes to tracked files first. Git will not make an empty commit.

Reading the history: git log

At a glance

The cabinet is only useful if you can read it. The command git log lists the commits, newest first.

Each entry shows the commit's name, a string of 40 letters and digits, with its author, its time and its message. The option --oneline gives one short line each, and -n with a number shows only the latest few. A commit's name is worked out from everything it holds, so changing anything gives a new name, and its first 7 characters are usually enough to name it.

Take a guess: Has anyone ever made two different files with the same SHA-1 name?

The cabinet is only useful if you can read it. git log lists the commits, newest first, each with its name, its author, its time and its message:

In a repository whose orders.txt has three commits

git log
commit 2b66da69ddfde9d5c043bb169b41e7f794a6e180 (HEAD -> main)
Author: Asha <asha@tower.example>
Date:   Thu Jan 1 10:00:05 2026 +0000

    Open gate 6 on Sundays

commit f0ee597234806c7700685be2ed5d51af1c31ec69
Author: Asha <asha@tower.example>
Date:   Thu Jan 1 10:00:04 2026 +0000

    Close gate 2 for repairs

commit 9c23f356257b2ea5541393ed65855662c6dcf0e2
Author: Asha <asha@tower.example>
Date:   Thu Jan 1 10:00:03 2026 +0000

    First orders

Each commit starts with its name, a long string of 40 letters and digits. Git works it out from everything the commit holds: the files, the message, the author, the time, and the commit before it. Change any of those, and the name changes completely. That is why a commit's name can be trusted: no two different commits share one.

Commit name

The 40-character name of a commit, written in hexadecimal, the digits 0 to 9 and the letters a to f. Git's manual calls such a name an object name, and people often call it a hash or a SHA-1, after the method used to work it out. Its first 7 characters are usually enough to name it.

The words in brackets on the first commit, HEAD -> main, say that this commit is the latest on main, and that main is where you are now. The branches lesson explains both.

Shorter forms

For a quick look, --oneline gives each commit on one line, with the short form of its name and the subject only. -n 2, or -2, shows only the newest two:

In a repository whose orders.txt has three commits

git log --oneline
git log --oneline -n 2
2b66da6 (HEAD -> main) Open gate 6 on Sundays
f0ee597 Close gate 2 for repairs
9c23f35 First orders
2b66da6 (HEAD -> main) Open gate 6 on Sundays
f0ee597 Close gate 2 for repairs

Where does git keep track of the latest commit on main? In a one-line text file in .git, which holds the full name:

In a repository whose orders.txt has three commits

git log --oneline -1
cat .git/refs/heads/main
2b66da6 (HEAD -> main) Open gate 6 on Sundays
2b66da69ddfde9d5c043bb169b41e7f794a6e180
WrittenShows
git logevery commit, newest first, in full
git log --onelineone line for each commit
git log -n 3 or git log -3only the newest three
git log -1only the latest

Worked example 5

Corin asks when gate 2 was closed, by whom, and why. Find the answer in the history, without opening the orders.

What we have: a repository with three commits

What we need: the commit that closed gate 2, with its author and time

The plan: git log --oneline finds the subject quickly; git log shows the author and time of each

In a repository whose orders.txt has three commits

git log --oneline
2b66da6 (HEAD -> main) Open gate 6 on Sundays
f0ee597 Close gate 2 for repairs
9c23f35 First orders
  1. The second line is the one: f0ee597, Close gate 2 for repairs. In the full git log above, that commit's author is Asha, at 10:00:04 on 1 January 2026.

Gate 2 was closed by Asha, in commit f0ee597, for repairs.

Check: the full log shows the same name, f0ee597..., with its author and date.

A path that fails: reading orders.txt itself. It says that gate 2 is closed, but not when, by whom, or why.

When you are stuck: the history is hard to read

Too long: --oneline, or -n with a number.

Empty: no commit has been made yet, and git log says so.

A commit's name is long: its first 7 characters are enough to name it.

Newest first: the first line is the latest commit, not the first one.

Your turn

How many characters long is a commit's full name?Tap to reveal
  1. The glossary says it is written in hexadecimal.

40.

Which command shows only the latest commit, on one line?Tap to reveal
  1. --oneline, and a count of 1.

git log --oneline -1.

git log --oneline -n 1
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
2b66da6 (HEAD -> main) Open gate 6 on Sundays
  1. -n 1 keeps only the newest commit.
  2. --oneline gives its short name and subject.

It prints 2b66da6 (HEAD -> main) Open gate 6 on Sundays.

Did you know?

SHA-1 was broken in 2017

The names git gives are hard to fake, but not impossible. On 23 February 2017 Google announced the first SHA-1 collision: two different files with the same SHA-1, the method behind git's names. Git's manual says this attack, called SHAttered, showed a practical collision, and that from version 2.13.0 git uses a hardened SHA-1 that the attack cannot fool. Git still plans to move to a newer hash.

Why it fits here: It answers this topic's question: in 2017 two different files were given one SHA-1, and git now uses a hardened version.

Sources: Google Security Blog: Announcing the first SHA1 collision; git manual: hash-function-transition

Seven characters, or more

Seven characters is a starting point, not a rule. The git config manual says that, unless set, the length of short names is worked out from roughly how many objects the repository holds, so that they stay unique for some time. The minimum is 4. So a large project shows longer short names than a small one.

Why it fits here: It refines this topic's 7 characters: git lengthens short names as a repository grows, so that they stay unique.

Source: git manual: git-config (core.abbrev)

In one line

git log lists the commits, newest first, with each one's 40-character name, author, time and message; --oneline and -n give shorter views. A commit's name is worked out from everything it holds, so no two commits share one.

Telling git who you are

At a glance

Every commit records an author: a name and an email. Git takes them from its settings, and on a new machine it has none.

So before your first commit, you tell git who you are, once on each machine: git config --global user.name and git config --global user.email. The email is stored in every commit, for anyone who can read the repository. Setting init.defaultBranch to main names the first branch of each new repository. The command git --version checks that git is installed, and git log shows each commit's author.

Take a guess: Why does a new repository need a setting to start on a branch named main?

Every commit records an author: a name and an email. Git takes them from its settings, and on a new machine it has none. So before your first commit on a real machine, you tell git who you are, once. First check that git is installed, and which version:

git --version
git version 2.53.0

Then set your name and email. The --global option keeps them in your own settings file, ~/.gitconfig, for every repository you work in. These lines change a real machine's settings, so they have no Run button here:

Once, on a real machine

git config --global user.name "Asha Rao"
git config --global user.email "asha@example.com"
git config --global init.defaultBranch main

The third line sets the name of the first line of work in every new repository to main. Many teams and GitHub use that name; without the setting, git may choose another and print a hint about it.

Author

The person who made a change, recorded in each commit as a name and an email, from the settings user.name and user.email. Git also records a committer, the person who filed the commit, which is usually the same.

On this page, git is already set up with the name Asha and the email asha@tower.example, which is why every commit so far says so. git log shows the author of each commit:

In a repository whose orders.txt has three commits

git log -1
commit 2b66da69ddfde9d5c043bb169b41e7f794a6e180 (HEAD -> main)
Author: Asha <asha@tower.example>
Date:   Thu Jan 1 10:00:05 2026 +0000

    Open gate 6 on Sundays

Which email?

The email is stored in every commit, and anyone who can read the repository can read it. For work that will be public on GitHub, GitHub's documentation offers a noreply address of its own to use instead; the GitHub lesson of this step comes back to it.

SettingHoldsSet with
user.nameyour name, as it appears on commitsgit config --global user.name "Your Name"
user.emailyour email, on every commitgit config --global user.email "you@example.com"
init.defaultBranchthe first branch's name in new repositoriesgit config --global init.defaultBranch main

Worked example 6

A new laptop has git, but nothing has been committed on it yet. List the steps, in order, to be ready for a first commit, and say how to check the result.

What we have: a new machine with git installed

What we need: git's version known, the identity set, and a way to check it

The plan: git --version first; then the three settings; then a first commit, whose Author line git log shows

  1. Check git: git --version prints its version.
git --version
git version 2.53.0
  1. Set the name, the email and the first branch's name with the three git config --global lines above.
  2. Make a first commit in any repository, then read its Author line with git log -1.

git --version, then git config --global for user.name, user.email and init.defaultBranch.

Check: git log -1 shows Author: with the name and email that were set.

A path that fails: leaving out --global, outside a repository. Without --global, git config changes the settings of the repository you are in; outside any repository it stops with fatal: not in a git directory, and status 128.

When you are stuck: commits show the wrong name, or git asks who you are

No name or email set: run the two git config --global lines, with your own details.

The wrong name on commits: the settings were set with other details; set them again.

Different details for one project: run git config without --global inside that repository.

Not sure what is set: git config --global --list prints your settings, on a real machine.

Your turn

Which two settings decide the author of your commits?Tap to reveal
  1. Both start with user.

user.name and user.email.

What does --global do in git config?Tap to reveal
  1. It chooses where the setting is kept.

It keeps the setting in your own settings file, for every repository.

Why does every commit on this page show Asha?Tap to reveal
  1. Git here was set up before the lesson.

Because this page's git is already set up with that name.

Did you know?

Settings in layers

Git reads its settings from several files, one after another. The git config manual lists them: the system-wide file, then your own global ones, then the repository's own. It says the last value found takes precedence over those read earlier. So git config user.email in one repository, without --global, gives that project its own address, while your other projects keep yours.

Why it fits here: It adds to this topic's --global: a setting made inside one repository wins over your global one there.

Source: git manual: git-config (FILES)

main by choice since Git 2.28

Git did not always let you choose the first branch's name. GitHub's notes on Git 2.28, written on 27 July 2020, say git init had always created a first branch named master. Version 2.28 brought the setting init.defaultBranch, and git init uses its value when it is set. That is why git config --global init.defaultBranch main makes every new repository start on main.

Why it fits here: It answers this topic's question: since Git 2.28, init.defaultBranch decides the first branch's name, which was always master before.

Source: GitHub Blog: Highlights from Git 2.28

In one line

Every commit records an author from the settings user.name and user.email; set them once on each machine with git config --global, with init.defaultBranch main as well. git --version checks the installation, and git log shows each commit's author.

Where it is used

Git is part of nearly every developer's day. Here is where each idea of this lesson does its work.

What you learnedWhere it is usedA real example
version controlany project worth keepinga website's code, a book, a set of settings
git initstarting a projectthe first command in a new folder
git statusbefore and after every changechecking what a commit would hold
the staging areachoosing what goes into a commitcommitting one finished change, not the trial beside it
git commit -m "why"recording each finished stepFix the price shown on the order page
git logfinding when and why something changedthe commit that broke the page
git config --globala new machinethe first hour on a new laptop

Mistakes and debugging

These are the slips that people meet most often in their first days with git. Every output below is the real one, from the code shown.

1. Running git outside a repository

git log
fatal: not a git repository (or any of the parent directories): .git

The fix: cd into the project's folder, or run git init there if it is new.

2. A misspelt file name

git init -q
git add order.txt
fatal: pathspec 'order.txt' did not match any files

Git calls the names you give it pathspecs. The fix: check with ls, or let Tab write the name.

3. Expecting -a to add a new file

In a folder that holds orders.txt, the sluice orders, with one line

git init -q
git add orders.txt
git commit -q -m "First orders"
printf 'pump 7 low\n' > notes.txt
git commit -am "Add notes"
echo $?
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	notes.txt

nothing added to commit but untracked files present (use "git add" to track)
1

The fix: git add notes.txt once, then commit.

4. Reading the history of an empty repository

git init -q
git log
fatal: your current branch 'main' does not have any commits yet

The fix: make a first commit; there is nothing to show until then.

5. A message that says nothing

This one prints no error, which is why it does the most harm. A history full of messages such as fix, update or changes cannot answer the question it exists for: why. The fix: a subject that reads as an order, and a body with the reason when the reason is not obvious from the change.

The debugging corner: git status first

Whenever git surprises you, run git status and read every line, hints included. It says where you are, what is on the tray, what has changed on the desk, and what git does not know about, and its hints name the command you probably want.

Quick card

The whole lesson on one card. Come back to it whenever you need a reminder.

The lesson in code

git init -q                          # start a repository here
git status                           # what git sees
git add orders.txt                   # put the file on the tray
git commit -q -m "First orders"      # file a snapshot, with why
echo "close gate 2" >> orders.txt
git commit -q -am "Close gate 2"     # stage what changed, and file it
git log --oneline                    # the history, newest first
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	orders.txt

nothing added to commit but untracked files present (use "git add" to track)
2b93068 (HEAD -> main) Close gate 2
57f16e5 First orders
When you seeIt meansLook at
fatal: not a git repositorythis folder, and those above, have no .gitpwd, or git init
pathspec ... did not match any filesno file has that namels, Tab
Untracked filesfiles git has never been told aboutgit add
Changes not staged for commitchanges on the desk, not on the traygit add
Changes to be committedon the tray, ready to filegit commit
nothing to commit, working tree cleaneverything is filednothing to do
does not have any commits yetan empty repositorya first commit
(root-commit)the first commit of a repositorynothing to fix

The story: One sheet, many drawers

You open a drawer for the orders and tell it to watch the sheet. Each time the sheet is right, you file a picture of it in the drawer, with your name, the time, and one line saying why it changed. When Corin asks for Tuesday, you read the drawer's list from the top and find it in a moment.

Eighteen copies go to the stove. The drawer holds every one of their stories anyway.

"Nothing in this Archive is thrown away," says the Keeper. "Only moved to an older drawer."

Reagents gathered: repository, commit, staging area, git init, git add, git log.

Sources