t0ul

Git and GitHub the Safe Way, Ch. 3: The Daily Loop

Part of Git and GitHub the Safe Way, a short book on git, GitHub, and the habits that keep a repository from hurting you.

Chapter 1 said safety is mostly looking before each step. This chapter is the loop those looks live in. If you know add and commit already, the new parts are staging part of a file, writing a message that will still mean something next year, and the one habit that stops most secrets at the door.

The example is a recipe in a file called pancakes.md. Start a repository with git init in a folder of your own and create the file, or clone the book’s practice repository and work in recipes/pancakes.md. Type along; the text does not matter, the loop does.

The loop

Status, add, commit. Everything else is a variation.

git status --short
 M pancakes.md

M in the second column means modified in the working tree, not yet staged. The long form, git status without --short, says the same thing in sentences and reminds you of the commands; the short form is what you will type a hundred times a day once the sentences are familiar.

Before adding anything, look at what changed:

git diff
diff --git a/pancakes.md b/pancakes.md
index 992a249..7742d60 100644
--- a/pancakes.md
+++ b/pancakes.md
@@ -1,6 +1,6 @@
 # Pancakes
 
-Serves 4.
+Serves 4, or 2 hungry people.
 
 ## Ingredients
 
@@ -15,3 +15,4 @@ Serves 4.
 2. Add the flour and salt, whisk until smooth.
 3. Rest the batter for 20 minutes.
 4. Fry in a hot pan, one ladle at a time.
+5. Keep warm in a low oven while the rest cook.

Two separate edits in one file: a fix to the serving line near the top, and a new step at the bottom. They have nothing to do with each other, and they should not share a commit.

One change per commit

A commit that does one thing can be read in one glance, reverted on its own, and found later by its message. A commit called “updates” that touches nine things can do none of that, and when one of the nine turns out to be wrong you have to take back all nine.

git lets you stage part of a file. git add -p (“patch”) walks through each block of change, called a hunk, and asks:

git add -p pancakes.md
@@ -1,6 +1,6 @@
 # Pancakes
 
-Serves 4.
+Serves 4, or 2 hungry people.
 
 ## Ingredients
 
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? y
@@ -15,3 +15,4 @@ Serves 4.
 2. Add the flour and salt, whisk until smooth.
 3. Rest the batter for 20 minutes.
 4. Fry in a hot pan, one ladle at a time.
+5. Keep warm in a low oven while the rest cook.
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? n

y stages the hunk, n skips it, q quits, ? explains the rest. Now the status shows both letters:

git status --short
MM pancakes.md

Staged changes in the first column, unstaged in the second. git diff shows what is still unstaged; git diff --staged shows exactly what the next commit will contain, which is the glance that matters:

git diff --staged
diff --git a/pancakes.md b/pancakes.md
index 992a249..ddb0831 100644
--- a/pancakes.md
+++ b/pancakes.md
@@ -1,6 +1,6 @@
 # Pancakes
 
-Serves 4.
+Serves 4, or 2 hungry people.
 
 ## Ingredients
 

Only the serving line. Commit it, then stage and commit the other change on its own:

git commit -m "Say how many the recipe serves"
git add pancakes.md
git commit -m "Keep finished pancakes warm while the rest cook"
git log --oneline
a743d22 Keep finished pancakes warm while the rest cook
83f2c90 Say how many the recipe serves
1607fc3 Add the pancake recipe

Two commits, each readable from its first line. The habit this builds is bigger than tidiness. git add . stages everything in the folder, including the file you did not mean to create; git add -p makes you look at every line that is about to enter history. Chapter 4 adds a scanner to that glance. Between the two, almost nothing gets in by accident.

A message a stranger can use

The first line of a commit message is a short sentence, under about fifty characters, written as an instruction: “Add”, “Fix”, “Remove”, “Say”. It is what git log --oneline shows, what GitHub lists, and what a search will turn up in a year. “Fix bug” and “updates” are not messages; “Fix the off-by-one in the date parser” is.

When the why is not obvious from the change, add a body. The second -m starts a new paragraph:

git commit -m "Suggest buttermilk as a variation" -m "Tested twice this week. The batter needs five more minutes of rest with buttermilk, which is why this is a note rather than a change to the method."
git log -1
commit 502aa32cb7434b79f2b9c869ebe3382a2b1b6309
Author: Ada Gopher <ada@example.com>
Date:   Sun Oct 18 09:30:00 2026 -0400

    Suggest buttermilk as a variation
    
    Tested twice this week. The batter needs five more minutes of rest with buttermilk, which is why this is a note rather than a change to the method.

Without -m at all, git opens the editor from chapter 2 with a template; many people write every message that way once they stop fearing the editor. The body is where “why” lives. The diff already says what.

Reading history

git log --oneline

is the one you will use most. Three others earn their place:

  • git log -1 --stat shows the latest commit with the files it touched.
  • git show --stat HEAD~1 shows the one before; HEAD~3 is three back.
  • git log --oneline --graph --all draws branches, which becomes useful in chapter 5.

git log opens a pager when the output is longer than the screen; q leaves it.

A .gitignore per project

Chapter 2 set a global ignore file for the junk every machine produces. Each project also gets its own .gitignore, committed with the project, for the things this project produces: build output, downloaded data, the .env file that holds its settings. Patterns are one per line, * matches anything, a trailing / means a folder:

.env
*.log
dist/

Write it at the start of a project, before the first git add ., not after. Chapter 4 is about why the first line matters most.

Conclusion

Status, diff, add -p, diff --staged, commit, log. One change per commit, a first line someone can act on, a body when the reason is not in the diff. The glance at git diff --staged before every commit is the habit everything else in this book hangs on. Next: making sure what you glance at never contains a secret.

← Back to the blog