By the end of today you can
- Record the site’s source in a local Git repository, leaving generated output out
- Inspect changes, select them, and commit a meaningful checkpoint
- Distinguish unstaging a change from discarding an edit
- Find recent checkpoints and restore a changed file
Save all files and stop the Hugo preview before you start.
Check Git, in the right folder
Open a terminal in the folder with hugo.toml (or checkout companion branch chapter-05 in ssg-playground), then:
git --version
git rev-parse --show-toplevel- No version? Install Git from git-scm.com, then reopen the terminal
not a git repositoryis expected here: nothing manages this folder yet (playground users are already inside git)- A path instead? Git already manages it or a parent. Do not nest a new repository blindly
Create the repository
git init -b main- Creates the hidden
.gitfolder, where the history lives: leave it alone mainis the name of the branch, one line of development
Then set who you are, for this repository only:
git config user.name "Your Chosen Author Name"
git config user.email "[email protected]"Your identity is public
- The name and email become part of the history, which may later be shared
- They identify the author; they do not sign you in anywhere
- Using GitHub? Copy its exact private
noreplyaddress from your settings. Never invent one. - Changing it later does not rewrite old commits
Check with git config user.name and git config user.email.
Three places an edit can be
| Place | Meaning | Ask yourself |
|---|---|---|
| Working tree | The files you edit on disk | What have I changed? |
| Staging area | Prepared for the next commit | What am I about to record? |
| Commit history | Recorded project states | What did I save earlier? |
Save updates the file. git add stages it. git commit records what is staged.
Edit a file after staging it, and the new edit is not included until you stage again.
Make a content change
At the end of your first article, after a blank line:
## My publishing checklist
- Read the page in the local preview.
- Check its links and image description.
- Review the changed files before recording a checkpoint.Check it in hugo server: a diff shows the text, the preview shows how it renders.
Stage, and watch it move
git add content/articles/first-learning-note/index.md
git diff --cached -- content/articles/first-learning-note/index.md
git diff -- content/articles/first-learning-note/index.mdThe last command now prints nothing.
The change has not vanished: it moved to the staged comparison.
Make a mistake on purpose
Start from a clean git status. In the body rule of static/css/site.css:
font-size: 6rem;The site still builds; the text is absurdly large. Do not stage or commit it.
git diff -- static/css/site.css
git restore -- static/css/site.css
git statusThe text returns to normal, and the checklist is still committed.
Two similar commands
| Command | What happens |
|---|---|
git restore --staged -- file | Unstages it; keeps your edit |
git restore -- file | Replaces the file with its staged version; discards unstaged edits |
- The second discards every unstaged edit in that file, not just one line
- Staged the mistake by accident? Unstage it first, then restore
- Good work and a mistake in one file? Fix the line in the editor instead
Read your history
git log --oneline -5
git diff HEAD~1 HEAD -- content/articles/first-learning-note/index.md- Two commits, newest first: the checklist, then the Chapter 5 baseline
HEADis the current commit;HEAD~1the one before it- Your identifiers differ from everyone else’s
History starts when you start committing: Chapters 1 to 5 are one baseline.
Try it yourself
Improve one sentence in content/about/index.md, keeping its front matter and links:
- Save and check the About page in the preview
git statusand a file-specificgit diff- Stage it and check the staged diff
- Commit with a message that explains the improvement
- Status is clean; the commit is in the log
When something goes wrong
| What you see | What to check |
|---|---|
not a git repository after setup | The terminal’s folder |
| Git asks who you are | Set user.name and user.email |
git diff is empty after an edit | It may be staged: try --cached |
nothing to commit | Unsaved, ignored, unstaged, or already committed |
| LF and CRLF warnings | Newline styles: check the diff |
Completion check
- Git manages the Hugo folder I intended
- The first commit holds the source and image, without generated output
- I can tell saving, staging, and committing apart
- I committed the publishing checklist after reviewing it
- I unstaged a change without losing the edit
- I discarded the CSS experiment and checked the result
- My About-page commit is in the log, and the tree is clean
Your latest commit is the Chapter 6 checkpoint: no backup folder needed.