Loading

T7 Git and GitHub · Lesson 2

The basic workflow

The cycle of everyday work: look at what changed with git status and git diff, choose what goes into each commit, undo a change before it is committed with git restore, remove and rename tracked files, keep files out of the repository with .gitignore, and look back at any commit with git show.

  • follow the everyday cycle: edit, look, stage, commit
  • read a diff, and compare the desk, the tray and the last commit
  • undo a change with git restore, and unstage one with git restore --staged
  • remove and rename tracked files with git rm and git mv
  • keep files out of the repository with .gitignore
  • look back at a commit with git show, and read git status -s
About 50 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

Everyday work with git has a rhythm, like a carpenter who measures twice and cuts once. Change something, look at exactly what changed, keep only what is finished, and file it with a reason.

These pages show that cycle, and how to read a diff, the list of lines that changed. They show how to undo a change before it is filed, and how to remove and rename tracked files. They show how to keep files out of git with .gitignore, and how to look back at any commit you have made.

Before you start

The story: The hasty filing

Corin has learned to file pictures of the orders, and he files them fast. This morning he filed the orders together with a scribbled note to himself, and a change to gate 2 that he had only been trying out.

Now the drawer says, with his name on it, that gate 2 opens at dawn. It does not.

Keeper Hollis taps the drawer. "Look at what changed before you file it," she says. "Choose what goes in. And some papers never go in at all."

The last lesson filed the first versions. This one is about every day after: changing files, looking at exactly what changed, choosing what goes into each commit, and undoing what should not. Most of a developer's git is this cycle, many times a day.

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
The three places.desk, tray, cabinetEvery command here moves a change between them.
git status.git statusNow with a short form.
git add and git commit.git commit -m "why"Done with care, one change at a time.
Commit names.aba517bgit show takes them.
Patterns.*.log.gitignore uses them.
Hidden files..envFiles that must never be committed.
sed and >>.echo "x" >> fileTo change a file between commits.
rm and mv.mv a.txt b.txtgit has its own forms of them.
Pipes.| head -n 5To shorten a long answer.
Lines of a file.cat -nA diff is about lines.

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

  • follow the everyday cycle: edit, look, stage, commit;
  • read a diff, and compare the desk, the tray and the last commit;
  • undo a change with git restore, and unstage one with git restore --staged;
  • remove and rename tracked files with git rm and git mv;
  • keep files out of the repository with .gitignore;
  • look back at a commit with git show, and read git status -s.

How to follow this lesson

Most blocks start in a repository whose orders.txt holds three lines and one commit: open gate 4 at noon, close gate 2, check gate 5. Each block changes it in its own way, so you can run them in any order.

In one line

This lesson is the everyday cycle: look at what changed, choose what to commit, undo what should not stay, and keep some files out altogether.

The everyday cycle

At a glance

A carpenter measures twice and cuts once. Working with git has the same rhythm: change, look, choose and file.

You change a file, then look at what changed with git status -s and git diff. You stage only what is finished with git add, and commit it with a reason. Then the next change begins. In the short status, the first letter shows the tray and the second the desk: M in the second column means changed on the desk, not yet staged.

Take a guess: Can you stage only part of a file's changes?

A carpenter measures twice and cuts once. Working with git has the same rhythm: change something, look at exactly what changed, put on the tray only what is finished, and commit it with a reason. Then the next change. Here is one whole turn of that cycle, for one new line:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "open gate 6" >> orders.txt
git status -s
git diff
 M orders.txt
diff --git a/orders.txt b/orders.txt
index 060e3cf..da8dc82 100644
--- a/orders.txt
+++ b/orders.txt
@@ -1,3 +1,4 @@
 open gate 4 at noon
 close gate 2
 check gate 5
+open gate 6

Follow it step by step. The change is made on the desk. git status -s gives a short status: the M in the second column says that orders.txt has changed on the desk. git diff shows the change itself: one line added, marked with a +. The next section reads a diff line by line. The worked example below takes the same change the rest of the way: onto the tray, and into a commit.

The short status

git status -s prints one line for each file, with two letters in front: the first for the tray, the second for the desk. A space means no change there:

LettersMeans
Mchanged on the desk, not yet on the tray
M changed, and the change is on the tray
MMon the tray, and changed again on the desk since
A a new file, on the tray
D removed, and the removal is on the tray
R renamed, on the tray
??a file git does not track

Worked example 1

The orders need a new line, open gate 6. Take the change through the whole cycle, checking at each step that git sees what you expect, and finish with the history.

What we have: a repository with one commit, and one line to add

What we need: the change committed, with a check at every step

The plan: change, short status, diff, add, short status, commit, log

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "open gate 6" >> orders.txt
git status -s
git diff
git add orders.txt
git status -s
git commit -q -m "Open gate 6"
git log --oneline
 M orders.txt
diff --git a/orders.txt b/orders.txt
index 060e3cf..da8dc82 100644
--- a/orders.txt
+++ b/orders.txt
@@ -1,3 +1,4 @@
 open gate 4 at noon
 close gate 2
 check gate 5
+open gate 6
M  orders.txt
187cb9d (HEAD -> main) Open gate 6
aba517b First orders
  1. Each look confirmed the step before it: the change was on the desk, then on the tray, then in the history.

The line is in commit 187cb9d, Open gate 6.

Check: the log shows two commits, the new one on top.

A path that fails: committing without looking. The command git commit -a would have filed every change on the desk, wanted or not; the look before the add is what catches the unwanted ones.

When you are stuck: you have lost track of what is where

git status -s: the first letter is the tray, the second the desk.

git diff: what changed on the desk since the last git add.

git log --oneline: what is already filed.

When in doubt, look before you add, and look again before you commit.

Your turn

What does M orders.txt mean in git status -s, with the space first?Tap to reveal
  1. The first column is the tray; a space means nothing there.
  2. The second column is the desk.

orders.txt has changed on the desk, and is not yet staged.

And M orders.txt, with the M first?Tap to reveal
  1. The first column is the tray.

The change is on the tray, ready to commit.

Name the four steps of the everyday cycle.Tap to reveal
  1. Change, look, choose, file.

Edit, look (status and diff), stage (git add), commit.

Did you know?

Staging half a file

git add can take part of a file. The git add manual says its -p option lets you choose hunks, the pieces of a change, between the desk and the tray, and add only the ones you pick. It gives you a chance to review each difference before it is staged. So two unrelated edits in one file can still go into two separate commits.

Why it fits here: It answers this topic's question: git add -p stages a file one piece at a time, so a commit holds only what is finished.

Source: git manual: git-add

One commit per issue

The Pro Git book gives a rule for commits: make each one a logically separate changeset. It warns against coding all weekend on five different issues and then filing them as one massive commit on Monday. Even then, it says, use the staging area on Monday to split the work into at least one commit per issue.

Why it fits here: It shows why this topic's cycle stages only what is finished: each commit should hold one separate change.

Source: Pro Git: Contributing to a Project

In one line

The everyday cycle is: change a file, look with git status -s and git diff, stage what is finished with git add, and commit it with a reason. In the short status, the first letter is the tray and the second the desk.

Reading a diff

At a glance

A diff is a list of the lines that changed between two versions of a file. Git prints it in a standard form that many tools share.

A line starting with - was removed, a line starting with + was added, and a line starting with a space is unchanged context. The changes come in pieces called hunks, each headed by @@ with the line numbers in the old and new versions. Plain git diff compares the desk with the tray, and git diff --staged compares the tray with the last commit.

Take a guess: Why does a diff show three unchanged lines around each change?

A diff is a list of the lines that changed between two versions of a file. Git prints it in a standard form that many tools share, and once you can read one, you can read them all. Here one line was changed:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

sed -i 's/close gate 2/open gate 2/' orders.txt
git diff
diff --git a/orders.txt b/orders.txt
index 060e3cf..a57c524 100644
--- a/orders.txt
+++ b/orders.txt
@@ -1,3 +1,3 @@
 open gate 4 at noon
-close gate 2
+open gate 2
 check gate 5
LineMeans
diff --git a/orders.txt b/orders.txta diff of one file; a is the old version, b the new
index 060e3cf..a57c524 100644the short names of the two versions of the file, and its mode
--- a/orders.txt, +++ b/orders.txtwhich side is old (-) and which is new (+)
@@ -1,3 +1,3 @@a hunk: from line 1, 3 lines of the old version became 3 lines of the new
a line starting with a spaceunchanged, shown so that you can see where you are
a line starting with -removed
a line starting with +added

Git has no idea of a changed line: a change is a line removed and a line added. A long file gives several hunks, each with up to three unchanged lines around its changes:

In a repository whose n.txt holds the numbers 1 to 20, one a line

sed -i 's/^5$/five/; s/^15$/fifteen/' n.txt
git diff
diff --git a/n.txt b/n.txt
index 0ff3bbb..c9fbe27 100644
--- a/n.txt
+++ b/n.txt
@@ -2,7 +2,7 @@
 2
 3
 4
-5
+five
 6
 7
 8
@@ -12,7 +12,7 @@
 12
 13
 14
-15
+fifteen
 16
 17
 18

When a hunk starts below line 1, git may write some text after the second @@. It is the nearest line above the hunk, in the old version, that starts with a letter (or with _ or $). In a program, that is often the first line of the function being changed, so it tells you where you are. The numbers above start with digits, so they got none. Here, line 1 is above the hunk:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "open gate 6" >> orders.txt
git commit -q -am "Open gate 6"
echo "close gate 4" >> orders.txt
git diff | tail -n 5
@@ -2,3 +2,4 @@ open gate 4 at noon
 close gate 2
 check gate 5
 open gate 6
+close gate 4

Which two versions?

Plain git diff compares the desk with the tray: the changes not yet staged. Once a change is added, plain git diff no longer shows it. git diff --staged compares the tray with the last commit, which is exactly what the next commit would add:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "open gate 6" >> orders.txt
git add orders.txt
git diff
git diff --staged
diff --git a/orders.txt b/orders.txt
index 060e3cf..da8dc82 100644
--- a/orders.txt
+++ b/orders.txt
@@ -1,3 +1,4 @@
 open gate 4 at noon
 close gate 2
 check gate 5
+open gate 6

The first git diff printed nothing at all: the desk and the tray agree. The second showed the staged line. A new file shows its whole text as added, from nothing, written /dev/null:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

printf 'pump 7 low\n' > notes.txt
git add notes.txt
git diff --staged
diff --git a/notes.txt b/notes.txt
new file mode 100644
index 0000000..e6b3664
--- /dev/null
+++ b/notes.txt
@@ -0,0 +1 @@
+pump 7 low
CommandComparesShows
git diffthe desk with the traywhat git add would stage
git diff --stagedthe tray with the last commitwhat git commit would file
git diff filethe same, for one fileone file's changes

Worked example 2

Corin has edited the orders and says that he only changed gate 2. Check what he actually changed, before anything is staged.

What we have: orders.txt, changed on the desk

What we need: every changed line, read from the diff

The plan: git diff shows the desk against the tray; read each - and + line

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

sed -i 's/close gate 2/open gate 2 at dawn/' orders.txt
echo "note: ask Corin" >> orders.txt
git diff
diff --git a/orders.txt b/orders.txt
index 060e3cf..9c09a62 100644
--- a/orders.txt
+++ b/orders.txt
@@ -1,3 +1,4 @@
 open gate 4 at noon
-close gate 2
+open gate 2 at dawn
 check gate 5
+note: ask Corin
  1. Two changes, not one: gate 2 now opens at dawn instead of closing, and a note to himself was added at the end. The note should not be filed.

He changed gate 2 and also added a note that does not belong in the orders.

Check: the hunk header says 3 old lines became 4: one line more.

A path that fails: trusting the description and committing with -a. The note to himself would be filed in the orders, with his name on it.

When you are stuck: a diff is hard to read, or shows nothing

Nothing at all: the change is already staged. Try git diff --staged.

A changed line appears twice: as - for the old and + for the new; that is how git shows a change.

Lost in a long diff: give git diff a file name, or pipe it into head.

The numbers in @@ -1,3 +1,4 @@ are a start line and a line count, for the old and the new version.

Your turn

In a diff, what does a line that starts with - mean?Tap to reveal
  1. - is the old side.

A line removed.

You staged a change, and git diff prints nothing. Where is the change?Tap to reveal
  1. Plain git diff compares the desk with the tray.

On the tray: see it with git diff --staged.

echo "open gate 6" >> orders.txt
git diff | tail -n 1
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
+open gate 6
  1. The added line comes last in the hunk.
  2. It is marked with +.

It prints +open gate 6.

Did you know?

diff since 1976

The program diff is decades older than git. In July 1976, J. W. Hunt of Stanford University and M. D. McIlroy of Bell Laboratories described its method in a technical report, An Algorithm for Differential File Comparison. The report says diff gives the differences between two files as a minimal list of line changes, enough to bring either file into agreement with the other.

Why it fits here: It shows how old this topic's diff is: its method was published by Hunt and McIlroy in 1976.

Source: J. W. Hunt and M. D. McIlroy: An Algorithm for Differential File Comparison

Three lines of context

The three unchanged lines around each change have a purpose. Git's manual says diffs carry 3 lines of context by default, unless diff.context or the -U option says otherwise. The GNU diff manual gives the reason: patch, the program that applies a diff to another copy of a file, typically needs at least three lines of context to work properly.

Why it fits here: It answers this topic's question: three lines of context let the patch program apply a diff properly.

Sources: git manual: git-config (diff.context); GNU Diffutils manual: Unified Format

In one line

A diff lists changed lines: - for removed, + for added, a space for unchanged context, in hunks headed @@ -old +new @@. Plain git diff compares the desk with the tray; git diff --staged compares the tray with the last commit.

Undoing before you commit: git restore

At a glance

Not every change deserves to be kept: a trial that did not work, a note in the wrong file, a file staged by mistake. The command git restore takes each place back one step.

The command git restore file puts the file on the desk back to the tray's version, which is the last commit when nothing is staged. The change is thrown away, and nothing brings it back. With --staged, git restore takes a change off the tray instead and keeps it on the desk. The hints that git status prints name both.

Take a guess: Is there a way to put changes aside without throwing them away?

Not every change deserves to be kept. A trial that did not work, a note scribbled into the wrong file, a file staged by mistake: git restore takes each place back one step. It works on the desk, or with --staged, on the tray.

Throwing away a change on the desk

git restore file puts the file on the desk back to the version on the tray, which, when nothing is staged, is the last commit:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "gate 2 at dawn" >> orders.txt
git restore orders.txt
git status
cat orders.txt
On branch main
nothing to commit, working tree clean
open gate 4 at noon
close gate 2
check gate 5

There is no undo for git restore

A change on the desk that was never added or committed is not in git anywhere. The command git restore throws it away for good, as rm does. Look at git diff first, and be sure.

Taking a change off the tray

git restore --staged file puts the tray back to the last commit for that file. The change itself stays on the desk, so nothing is lost:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "gate 2 at dawn" >> orders.txt
git add orders.txt
git restore --staged orders.txt
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")

Notice that git status itself names both commands in its hints. When you forget which is which, git status reminds you.

You wantTypeWhat is lost
to take a file off the traygit restore --staged filenothing: the change stays on the desk
to throw away a change on the deskgit restore filethat change, for good
both, for one file--staged first, then plain restorethe change, for good

Worked example 3

Corin staged two things by mistake: a new notes.txt, which should stay on his desk untracked, and a trial change to orders.txt, close gate 4, which should be thrown away. Put things right, and check.

What we have: both files on the tray

What we need: notes.txt off the tray but kept, and the trial gone from orders.txt

The plan: take both off the tray with restore --staged; then throw the trial away with plain restore, which now copies the last commit's version back to the desk

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "close gate 4" >> orders.txt
printf 'pump 7 low\n' > notes.txt
git add orders.txt notes.txt
git restore --staged notes.txt orders.txt
git restore orders.txt
git status -s
?? notes.txt
  1. notes.txt is untracked again, ??, and still on the desk. orders.txt is not listed at all: it matches the last commit, so the trial is gone.

Only ?? notes.txt is left: the notes kept, the trial gone.

Check: git status -s lists nothing for orders.txt, because it is the same as the last commit.

A path that fails: running only plain git restore orders.txt while the trial is staged. It copies the tray to the desk, and the tray still holds the trial, so the trial stays, staged. The order is --staged first, then plain restore.

When you are stuck: git restore did not do what you expected

The change is still staged: plain restore copies the tray to the desk. Use --staged first.

The change is gone and you wanted it: if it was never committed, git cannot bring it back.

error: pathspec ... did not match: the name is wrong, or git has never tracked that file.

Not sure what will be lost: git diff for the desk, git diff --staged for the tray.

Your turn

Which command takes a file off the tray, and keeps its change on the desk?Tap to reveal
  1. The tray is the staging area.

git restore --staged file.

Can git bring back a change that git restore threw away, if it was never committed?Tap to reveal
  1. The change was never filed anywhere.

No.

echo "x" >> orders.txt
git restore orders.txt
git status -s
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
  1. git restore puts orders.txt back to the last commit.
  2. Nothing differs, so git status -s prints nothing.

It prints nothing.

Did you know?

git restore arrived in 2019

git restore is younger than many git guides. Git 2.23's release notes say two new commands, git switch and git restore, were introduced to split jobs out of the single git checkout command. GitHub's notes on that release, dated 16 August 2019, call them a new pair of experimental commands. Before then, git checkout did both jobs, which is why older guides use it here.

Why it fits here: It shows how new this topic's git restore is: it came with Git 2.23 in 2019, taking a job from git checkout.

Sources: git: release notes for 2.23.0; GitHub Blog: Highlights from Git 2.23

Put it aside with stash

A change need not be kept or thrown away; it can wait. The git stash manual says stash records the current state of the desk and the tray, then takes the desk back to the last commit. The saved changes can be listed, and later applied again, even on top of a different commit. So git stash is a safe pause where git restore would lose the work.

Why it fits here: It answers this topic's question: git stash puts changes aside, where git restore would throw them away.

Source: git manual: git-stash

In one line

The command git restore file throws away a change on the desk, back to the tray's version; nothing brings it back. The command git restore --staged file takes a change off the tray and keeps it on the desk. The hints of git status name both.

Removing and renaming: git rm and git mv

At a glance

Removing or renaming a tracked file is a change like any other, and it is filed in a commit like any other.

The command git rm file removes the file and stages its removal, while git rm --cached file keeps it on the desk but stops tracking it. The command git mv old new renames or moves a tracked file and stages the change. Git does not need to be told about renames: it works them out from the files' contents.

Take a guess: How alike must two files be for git to call the change a rename?

Removing or renaming a tracked file is a change like any other, and it is filed in a commit like any other. Git has its own forms of rm and mv, which change the desk and the tray in one step.

git rm

git rm file removes the file from the desk and puts its removal on the tray:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

git rm orders.txt
git status
ls
rm 'orders.txt'
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    orders.txt

The ls at the end printed nothing: the file is gone from the desk. The next commit will record that it was removed. The earlier versions stay in the history, as every filed version does.

With --cached, git stops tracking the file but leaves it on the desk. That is how you take out a file that was committed by mistake, such as a file of notes, and keep it:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

git rm --cached orders.txt
git status
ls
rm 'orders.txt'
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    orders.txt

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

orders.txt

git mv

git mv old new renames the file on the desk and on the tray together. Afterwards, git status calls it a rename. The commit's summary shows it with a percentage, which says how alike the old and new files are: here 100, as only the name changed:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

git mv orders.txt sluice.txt
git status
git commit -m 'Rename orders'
ls
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	renamed:    orders.txt -> sluice.txt

[main 0f13797] Rename orders
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename orders.txt => sluice.txt (100%)
sluice.txt

git does not store renames as such. It sees a file removed and a file added with the same contents, and works out that this was a rename. The small git on this page spots only exact renames; real git also spots a renamed file that was changed a little.

CommandOn the deskOn the tray
git rm filethe file is removedits removal is staged
git rm --cached filethe file staysits removal is staged; the file becomes untracked
git mv old newthe file is renamedthe rename is staged
git mv file folderthe file moves into the folderthe move is staged

Worked example 4

Tidy the Archive's table in one commit's worth of work: move old-notes.txt into a new folder named archive, and remove orders.txt, which is out of date. Check the short status before committing.

What we have: a repository with orders.txt and old-notes.txt, both committed

What we need: a move and a removal, both staged

The plan: make the folder; git mv the notes into it; git rm the orders quietly with -q; read git status -s

In a repository with orders.txt and old-notes.txt, both committed

mkdir archive
git mv old-notes.txt archive
git rm -q orders.txt
git status -s
R  old-notes.txt -> archive/old-notes.txt
D  orders.txt
  1. R in the first column is a staged rename, here into the archive folder; D is a staged removal. Both will go into the next commit.

R old-notes.txt -> archive/old-notes.txt and D orders.txt, ready to commit.

Check: both lines have their letter in the first column: on the tray.

A path that fails: using plain rm orders.txt and mv old-notes.txt archive. The desk changes, but nothing is staged: git status would show a deletion and an untracked file, and git add would be needed for both.

When you are stuck: a removal or rename does not behave

fatal: pathspec ... did not match any files: git rm only removes files git tracks.

You used plain rm or mv: stage the result with git add for the new name and git rm for the old.

You want to keep the file but stop tracking it: git rm --cached.

Removed by mistake, and not yet committed: git restore --staged, then git restore, brings it back.

Your turn

How do you stop tracking notes.txt, but keep the file on your desk?Tap to reveal
  1. --cached touches the tray only.

git rm --cached notes.txt.

What does (100%) mean after a rename in the commit's summary?Tap to reveal
  1. It says how alike the old and new files are.

The renamed file's contents are exactly the same.

git mv orders.txt sluice.txt
git status -s
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
R  orders.txt -> sluice.txt
  1. git mv stages the rename at once.
  2. R is a rename, in the first column, the tray.

It prints R orders.txt -> sluice.txt.

Did you know?

Half the same is a rename

Git decides afterwards whether a change was a rename. The git diff manual says a removed file and an added file count as a rename when they are similar enough, and that the default similarity is 50%. With -M90%, more than 90% of the file must be unchanged, and -M100% finds only exact renames. So a renamed file that was also lightly edited still shows as a rename.

Why it fits here: It answers this topic's question: by default, two files at least half alike count as a rename.

Source: git manual: git-diff

git mv is a convenience

git mv does nothing that three commands cannot. The Pro Git book explains that git does not track file movement, and stores no note saying that a file was renamed. Renaming with mv, then git rm on the old name and git add on the new, gives the same result, because git works out the change by itself. The book calls git mv a convenience: one command instead of three.

Why it fits here: It shows what this topic's git mv really does: the same as mv, git rm and git add, in one step.

Source: Pro Git: Recording Changes to the Repository

In one line

The command git rm removes a tracked file and stages the removal; --cached keeps it on the desk, untracked. The command git mv renames or moves a tracked file and stages the change. Git works out renames from contents.

Keeping files out: .gitignore

At a glance

Some files must never be committed: your own notes, the logs a program writes, the folder a build makes, and above all files that hold secrets such as passwords and keys.

A file named .gitignore lists them, one pattern a line, and git leaves them out of git status and git add. It is committed like any other file, so everyone on the project ignores the same things. It does not untrack a file that is already committed; git rm --cached does that, and the old commits still hold it.

Take a guess: Where can you find a ready-made .gitignore for your kind of project?

Some files must never be committed: your own notes, the logs a program writes, the folder that a build makes, and above all files that hold secrets such as passwords and keys. A file named .gitignore lists them, and git then leaves them out of git status and out of git add.

.gitignore

A text file in the repository, one pattern a line, naming files and folders that git should not track. It is committed like any other file, so everyone working on the project ignores the same things.

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

printf 'pump 7 low\n' > notes.txt
printf 'notes.txt\n' > .gitignore
git status
git add notes.txt
echo $?
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore

nothing added to commit but untracked files present (use "git add" to track)
The following paths are ignored by one of your .gitignore files:
notes.txt
hint: Use -f if you really want to add them.
hint: Disable this message with "git config set advice.addIgnoredFile false"
1

git status no longer mentions notes.txt, only the new .gitignore, which you would commit. Asked to add notes.txt by name, git refuses and says why. The hint mentions -f, for force, which adds it anyway; you will rarely want that.

Patterns

The lines of .gitignore are patterns, close to the shell's globs. A pattern with no slash matches a file or folder of that name anywhere in the repository; a slash at the end matches folders only, and everything inside them:

LineLeaves out
notes.txtevery file named notes.txt
*.logevery file whose name ends in .log
build/the folder build, with everything in it
.envthe file .env, where programs often keep secrets
# a commentnothing: lines starting with # are notes
!keep.lognothing: ! brings back a file that an earlier line left out

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

mkdir build
printf 'x\n' > build/out.txt
printf 'a\n' > pump.log
printf 'b\n' > gate.log
printf '*.log\nbuild/\n' > .gitignore
git status -s
git add .
git status -s
?? .gitignore
A  .gitignore

Two logs and a whole folder were made, yet git status -s listed only .gitignore, and git add . staged only .gitignore. The dot means everything here, but never what is ignored.

Ignoring a file does not untrack it

.gitignore only stops git from starting to track a file. A file that is already committed stays tracked. Take it out with git rm --cached, from the last section, then commit, and from then on the ignore rule keeps it out.

Worked example 5

A programmer's folder holds .env, with a secret in it, and pump.log, written by a program. Neither must ever be committed. Set up .gitignore, stage everything else with one command, and check what would be committed.

What we have: a repository with .env and pump.log on the desk

What we need: both left out, and .gitignore staged

The plan: write the two patterns into .gitignore; git add . stages everything that is not ignored; git status -s shows the result

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

printf 'secret\n' > .env
printf 'a\n' > pump.log
printf '.env\n*.log\n' > .gitignore
git add .
git status -s
A  .gitignore
  1. Only .gitignore was staged. The secret and the log are on the desk, unknown to git.

A .gitignore, and nothing else.

Check: neither .env nor pump.log appears, not even as ??.

A path that fails: writing .gitignore after committing .env. The secret is then in the history, and stays there even after the file is removed; anyone with a copy of the repository can read it.

When you are stuck: .gitignore does not seem to work

The file is still listed: it was already tracked. Run git rm --cached on it, then commit.

A folder is still listed: put a slash after its name, build/.

A pattern catches too much: a name without a slash matches at every level; start it with / to match only at the top.

Not sure why a file is ignored: git check-ignore -v file names the rule, on a real machine.

Your turn

Which .gitignore line leaves out every file ending in .tmp?Tap to reveal
  1. A pattern, as in the shell.

*.tmp.

Does adding a committed file's name to .gitignore stop git tracking it?Tap to reveal
  1. The rule only stops git from starting to track a file.

No: git rm --cached it as well.

printf 'a\n' > pump.log
printf '*.log\n' > .gitignore
git status -s
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
?? .gitignore
  1. pump.log matches *.log, so git leaves it out.
  2. .gitignore itself is a new, untracked file.

It prints ?? .gitignore.

Did you know?

Ready-made lists from GitHub

Nobody has to write a .gitignore from nothing. GitHub keeps a public collection of .gitignore templates, and its README says GitHub.com uses that list for the template chooser shown when you create a new repository or file. Its root folder holds templates for popular programming languages and technologies, and a folder named Global holds ones for editors, tools and operating systems.

Why it fits here: It answers this topic's question: GitHub's public collection of templates offers a starting .gitignore for most kinds of project.

Source: GitHub: gitignore templates (README)

A global ignore file of your own

Some patterns belong to you, not to a project. The gitignore manual says patterns for files your own tools make, such as an editor's backups, generally go in the file named by core.excludesFile. Unless set otherwise, that file is ~/.config/git/ignore. Patterns there apply in every repository, and nobody else has to share them.

Why it fits here: It adds to this topic's .gitignore: patterns for your own tools go in one global file that every repository reads.

Source: git manual: gitignore

In one line

A .gitignore file lists patterns of files and folders that git must not track: notes, logs, build output, and above all secrets. Both git status and git add leave them out. It does not untrack a file already committed; use git rm --cached for that.

Looking back: git show

At a glance

The command git log lists commits; git show opens one. It prints the commit's header, as git log does, and then the diff of what that commit changed.

With no name it shows the latest commit, HEAD. HEAD~1 is the one before it, HEAD~2 the one before that, and any commit can be named by the start of its name from git log --oneline. Together with git status -s and git diff, git show lets you see any change, before or after it is filed.

Take a guess: Can git show print a file as it was several commits ago?

git log lists commits; git show opens one. It prints the commit's header, as git log does, and then the diff of what that commit changed. With no name it shows the latest commit:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

git show
commit aba517bd0ac20cf6722879cf47522f787e978a68 (HEAD -> main)
Author: Asha <asha@tower.example>
Date:   Thu Jan 1 10:00:03 2026 +0000

    First orders

diff --git a/orders.txt b/orders.txt
new file mode 100644
index 0000000..060e3cf
--- /dev/null
+++ b/orders.txt
@@ -0,0 +1,3 @@
+open gate 4 at noon
+close gate 2
+check gate 5

This was the first commit, so every line of orders.txt is new. The command git show takes a commit's name, or its first few characters. It also takes a name counted back from HEAD: HEAD~1 is the commit before the latest, HEAD~2 the one before that:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "open gate 6" >> orders.txt
git commit -q -am "Open gate 6"
git show HEAD~1 | head -n 5
commit aba517bd0ac20cf6722879cf47522f787e978a68
Author: Asha <asha@tower.example>
Date:   Thu Jan 1 10:00:03 2026 +0000

    First orders

No branch name is shown in brackets this time, for two reasons. HEAD~1 is no longer the latest commit, so no branch points at it. Also, git writes such names only when its output goes to the terminal, as git log's manual page says. Into a pipe, such as | head, it leaves them out.

NameMeans
HEADthe latest commit on the current branch
HEAD~1the one before it
HEAD~2two before it
aba517ba commit by the start of its name

The short status, all together

Here is git status -s with one of each kind of line: a new file staged, a tracked file changed on the desk, and a file git does not track:

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "close gate 4" >> orders.txt
printf 'p\n' > new.txt
git add new.txt
printf 'u\n' > untracked.txt
git status -s
A  new.txt
 M orders.txt
?? untracked.txt

Worked example 6

A clerk wants to see exactly what the commit before the latest one put into the orders, without the latest change mixed in.

What we have: a repository with two commits

What we need: the diff of the earlier commit alone

The plan: git log --oneline to see both; git show HEAD~1 for the earlier one; tail keeps the end of the diff

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "open gate 6" >> orders.txt
git commit -q -am "Open gate 6"
git log --oneline
git show HEAD~1 | tail -n 3
8f039b6 (HEAD -> main) Open gate 6
aba517b First orders
+open gate 4 at noon
+close gate 2
+check gate 5
  1. HEAD~1 is aba517b, First orders, and its diff adds the first three lines, with nothing of gate 6, which came later.

git show HEAD~1 shows the first commit's three lines.

Check: the second line of git log names the same commit, aba517b.

A path that fails: git show with no name. It shows the latest commit, Open gate 6, not the one before.

When you are stuck: git show shows the wrong commit, or none

No name means the latest commit, HEAD.

Count back with HEAD~1, HEAD~2, or give the commit's name from git log --oneline.

unknown revision: the name or count does not exist; check git log --oneline.

Too long: pipe it into head, or less.

Your turn

Which commit does HEAD~2 name?Tap to reveal
  1. Each ~1 steps back one commit.

The commit two before the latest.

What does git show print after the commit's header?Tap to reveal
  1. It shows what the commit changed.

The commit's diff.

git show | head -n 1
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
commit aba517bd0ac20cf6722879cf47522f787e978a68
  1. git show starts with the commit's header, and its first line is the commit's full name.
  2. The output goes into a pipe, so git leaves out the branch name in brackets.

It prints commit aba517bd0ac20cf6722879cf47522f787e978a68, with no (HEAD -> main).

Did you know?

A tilde and a caret

HEAD~2 has a look-alike. Git's manual on naming revisions says a suffix ~n means the nth ancestor, following only first parents, so HEAD~3 is the same as HEAD^^^. A single ^ means the first parent, but ^2 means the second parent, which a merge commit has. So HEAD~2 and HEAD^2 look alike but name different commits.

Why it fits here: It sharpens this topic's HEAD~1: ~ counts back through first parents, while ^2 picks a merge's second parent.

Source: git manual: gitrevisions

One file, as it was

git show can open a single file as it was. The git show manual gives the example git show next~10:Documentation/README, which shows that file as it stood ten commits back on the branch next. In the same way, git show HEAD~2:orders.txt prints orders.txt as it was two commits ago, and changes nothing on the desk.

Why it fits here: It answers this topic's question: git show commit:file prints a file as it was in that commit.

Source: git manual: git-show

In one line

The command git show prints a commit's header and its diff: the latest by default, or any commit by its name or as HEAD~1, HEAD~2 and so on. With git status -s and git diff, it lets you see any change before or after it is filed.

Where it is used

This cycle is most of a developer's git, every day. Here is where each idea of this lesson does its work.

What you learnedWhere it is usedA real example
git status -sa glance before every commitchecking that only the intended files changed
git diffreading your own change before staging itspotting a debugging line left in by mistake
git diff --stageda last look before committingthe exact text a commit will hold
git restorethrowing away a failed trialputting a broken file back as it was
git mv, git rmtidying a projecta folder renamed in one commit
.gitignorekeeping secrets and clutter out.env, logs, build output
git showreviewing a changea colleague's commit, read line by line

Mistakes and debugging

These are the slips that people meet most often in the everyday cycle. Every output below is the real one, from the code shown.

1. Looking for a staged change with plain git diff

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "open gate 6" >> orders.txt
git add orders.txt
git diff
echo "[end]"
[end]

Nothing printed: the change is on the tray, and plain git diff compares the desk with the tray. The fix: git diff --staged.

2. Unstaging with plain git restore

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

echo "close gate 4" >> orders.txt
git add orders.txt
git restore orders.txt
git status -s
M  orders.txt

The change is still staged: plain restore copied the tray to the desk. The fix: git restore --staged orders.txt.

3. Removing a file that git does not track

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

printf 'x\n' > notes.txt
git rm notes.txt
fatal: pathspec 'notes.txt' did not match any files

git rm only removes files git tracks. The fix: plain rm notes.txt for an untracked file.

4. Ignoring a file after committing it

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

printf 'pump 7 low\n' > notes.txt
git add notes.txt
git commit -q -m 'Notes'
printf 'notes.txt\n' > .gitignore
echo 'more' >> notes.txt
git status -s
 M notes.txt
?? .gitignore

notes.txt was committed before .gitignore named it, so git still tracks it, and its changes still show. .gitignore only keeps out files that git does not track yet. The fix: git rm --cached notes.txt, then commit; the file stays on the desk, and from then on it is ignored.

5. Moving a file with plain mv

In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit

mv orders.txt sluice.txt
git status -s
 D orders.txt
?? sluice.txt

git sees a deletion and an unknown new file. The fix: git add -A, which stages both, and git then shows a rename; or use git mv in the first place.

The debugging corner: three looks

Before every commit, three looks: git status -s for which files, git diff for what is not yet staged, git diff --staged for exactly what the commit will hold. Most bad commits are caught by one of the three.

Quick card

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

The lesson in code

echo "open gate 6" >> orders.txt
git status -s                  # which files changed
git diff | tail -n 1           # the change on the desk
git add orders.txt             # onto the tray
git diff --staged | tail -n 1  # what the commit will hold
git commit -q -m "Open gate 6"
echo "oops" >> orders.txt
git restore orders.txt         # throw away a desk change
git mv orders.txt sluice.txt   # rename, staged
printf '*.log\n' > .gitignore  # never track logs
printf 'a\n' > pump.log
git status -s                  # the log is left out
git commit -q -m "Rename the orders"
git log --oneline
git show HEAD~1 | tail -n 1    # the last line that commit added
 M orders.txt
+open gate 6
+open gate 6
R  orders.txt -> sluice.txt
?? .gitignore
57ef5e3 (HEAD -> main) Rename the orders
187cb9d Open gate 6
aba517b First orders
+open gate 6
When you seeIt meansLook at
M in git status -schanged on the desk, not stagedgit diff, then git add
M in git status -sstagedgit diff --staged
??not trackedgit add, or .gitignore
git diff prints nothingno unstaged changes; maybe all stagedgit diff --staged
pathspec ... did not match any filesgit does not track that namegit status
The following paths are ignored.gitignore names the fileis that right?
renamed:, R a rename, worked out from contentsnothing to fix
D and ?? after mvplain mv was usedgit add -A

The story: Look, choose, file

You compare the sheet with its last picture, line by line, and see each change marked in or out. You put only the finished changes on the filing tray, undo the trial on gate 2, and write a list of papers that the drawer must never take, such as notes to yourself.

The next picture holds exactly what you meant, and nothing else.

Corin reads his own list of papers never to file. Most of them are his. He starts a second list.

Reagents gathered: working tree, staging, git diff, git restore, .gitignore, git show.

Sources