In the practice folder, you added the notes file to .gitignore after it was already in a commit, and Git kept tracking it until you ran git rm --cached. Even then, the earlier commit still holds the file. That was harmless there, but the store’s generic-grocery folder has a notes.txt too, and this one contains the back office Wi-Fi password.
In this lesson, you’ll write a .gitignore for the store’s site before it’s even a repository, then turn the site into one and check which rule ignores which file. After that, you’ll make the project’s first commits the right way around. Start by asking the project’s check script where things stand.
Write the Store’s .gitignore First
The store’s site still has no history, so every change you make to it would be as hard to trace as the three deals files you found at the start of the course. Open the generic-grocery folder in VS Code and open a terminal there. Then run the check script you met when you installed Git, node scripts/check.js. Its report starts where the last one ended:
Checking the Git repository in /Users/martin/projects/generic-grocery
✔ Git is installed (git version 2.43.0).
✘ This folder isn't a Git repository yet.
Next step: run git init in the project folder.
1 check(s) failed.
This is where you stopped last time. The script’s next step is git init, and you’ll run it soon, but not yet. Two files in this folder don’t belong in the store’s history. notes.txt has the Wi-Fi password, and .DS_Store is a file that macOS’s Finder creates to remember how a folder is displayed. VS Code hides .DS_Store in its Explorer, but Git would see it. It came along in the ZIP.
In the practice folder, after you’d committed the notes file and only then listed it in .gitignore, an edit to it still showed up in git status:
$ 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: notes.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
no changes added to commit (use "git add" and/or "git commit -a")
A .gitignore keeps only untracked files out of Git. The store’s folder isn’t a repository yet, so nothing in it is tracked. If the rules are in place before git init and the first git add, ignoring works completely, with nothing to undo.
A .gitignore file tells Git which files to keep out of the repository. It’s a plain text file in the project’s top folder, and each line in it is a pattern that names files Git should leave alone. In VS Code’s Explorer, click the New File icon at the top level of the project and name the file .gitignore, with the leading dot and no extension. Then type these lines:
.gitignore
# The previous developer's notes, with the Wi-Fi password
notes.txt
# System files from macOS and Windows
.DS_Store
Thumbs.db
# Editor settings and swap files
.vscode/
*.swp
# Packages that npm installs
node_modules/
Lines that start with # are comments for the people who read the file, and Git skips them along with blank lines. The comments are worth writing, because a .gitignore is easier to keep up to date when each rule says why it’s there.
The first group protects the secret. The second covers system files, .DS_Store from macOS and Thumbs.db, which Windows can create in folders with pictures. A pattern without a slash in it, like notes.txt, .DS_Store, and Thumbs.db, matches a file of that name in every folder of the project. That’s what you want here, because macOS drops a .DS_Store into any folder you open in Finder.
The other groups use two more kinds of patterns. A trailing slash, as in .vscode/, matches only a folder of that name and everything in it. Here, it keeps out the personal settings folder VS Code can create in a project. The * matches any characters, so *.swp ignores every file that ends in .swp, such as the swap files the Vim editor leaves behind as temporary recovery copies while you edit.