Your First Commit
Two commands, and Sandpiper has a history. This page runs the loop twice, because the second time is when it stops feeling like a ceremony and starts feeling like typing.
Stage it
git add puts a file's current contents into the staging area — the box from the last topic.
git add index.html git status
Git now lists index.html under changes to be committed. That phrase is Git's way of saying "in the box". Nothing has been recorded yet; you have only chosen what will be.
Commit it
git commit -m "Add the club home page"
Git answers with a line like [main (root-commit) a3f9c21] Add the club home page, followed by how many files changed. Read it: the branch you are on, a short identifier for the commit, and your message. Sandpiper now has exactly one commit, and its first version is recorded permanently.
The -m is the message option from Chapter 1. Leave it out and Git opens a text editor and waits for you to type one, which is where a great many beginners meet Vim unexpectedly and cannot get out. Use -m.
Do it again
One commit is a demonstration. Two is a history. Create style.css in the project — even a couple of lines is enough — and run the loop again:
git add style.css git commit -m "Add basic styling for the home page"
That is the rhythm. Change something, put the finished part in the box, record it with a sentence saying why. Everything else in this book is a variation on those two commands or a way of fixing them.
index.html making the journeyWhat git add . really means
You will see git add . everywhere. The dot means "this folder and everything below it", so the command stages every change Git can see.
It is convenient, and it is the reason people accidentally commit passwords, private notes and four hundred megabytes of junk. The command does exactly what it says; the problem is that most people run it without knowing what "everything" currently contains.
The habit that makes it safe is thirty seconds long: run git status first and read the list. Chapter 5 is the systematic fix, where you teach Git to stop offering you files that should never be committed at all.
Committing part of your work
Here is staging paying for itself the first time. Suppose you have edited three files but only two of them are finished:
git add index.html style.css git commit -m "Add the sightings section to the home page" git status
The commit contains two files. The third is still sitting in your working directory, exactly as you left it, ready to be finished and committed separately. Nothing was undone and nothing was rushed.
One thing that surprises everyone
Stage a file, then edit it again before committing. Run git status and the same file appears twice — once under changes to be committed, once under changes not staged.
Nothing has gone wrong. git add stages the file's contents at the moment you run it, not the file as a permanent subscription. The earlier version is in the box; the newer edit is still on the desk. Run git add again to update the box. This is behind most "I committed it but my change is missing" confusion in week two.
- "
git addadds a file to Git permanently." It adds the file's current contents to the next commit. Edit the file afterwards and that new edit is not staged, whichgit statuswill show you plainly. - "Committing sends my work somewhere." It writes to the
.gitdirectory on this machine. Nothing has left your laptop and nothing will until Chapter 7. - "
-mis optional decoration." Without it Git opens an editor and waits, which is where beginners get stranded in Vim. Every commit needs a message; the only choice is where you type it. - "
git add .is the professional way to do it." It is the common way. It is safe when you have just readgit statusand know what it will pick up, and it is how secrets get committed when you have not.
- Stage-then-commit is the highest-frequency operation in Git. Running it twice on this page means the rhythm is in your fingers before anything harder arrives.
- Knowing that
addcaptures contents rather than subscribing to a file prevents the most confusing bug a beginner can hit: a commit that is missing the change they were sure they made. - Committing two files out of three is the first moment staging is worth its extra command, and it is the shape of every good commit in Chapter 3.
Knowledge Check
You run git add index.html, then edit index.html again, then commit. What is recorded?
- The version of the file as it stood at the moment you ran
git add - The newest version, because Git re-reads the file when the commit is made
- Both versions, recorded as two separate changes inside the same commit
- Nothing, because Git refuses to commit a file that changed after being staged
What does the dot in git add . mean?
- This folder and everything below it, so every visible change gets staged at once
- The most recently modified file, as a shortcut for repeating the last add
- The repository root, regardless of which folder you happen to be standing in
- Any file that has already been committed once, so new files are left out
You have edited three files and only two are finished. What does staging let you do?
- Commit the two finished ones and leave the third untouched in your working directory
- Commit all three but mark the third as incomplete for a later commit
- Commit the two files and automatically undo the changes in the third
- Nothing yet, because a commit always records every file that has changed
Why does this book insist on -m rather than letting Git open an editor?
- Because the editor Git opens is often Vim, which beginners cannot easily exit
- Because a message typed with
-mis stored differently and searches faster - Because messages typed in an editor are optional and can be left empty
- Because
-mis the only way to write a message longer than one line
You got correct