Stop Tracking a File You Ignored

In the last lesson, you saw that every commit is a snapshot of every tracked file, not only the lines you changed. That’s why notes.txt sits in every snapshot since your second commit, even in the two commits that only changed prices. For a file of reminders that are only meant for you, that’s not where you want it.

In this lesson, you’ll keep notes.txt out of your future snapshots while the file stays on your disk for your own reminders. You’ll also see exactly which of your snapshots hold it. Start with the tool Git gives you for keeping files out of a repository, a .gitignore file.

Ignore the Notes File

Not every file in a project folder belongs in its history, and the store’s site will show you why soon. Its ZIP comes with its own notes.txt file, written by the previous developer, with the back-office Wi-Fi password in it. A file like that must never end up in a commit. Your practice folder is the right place to learn how Git keeps a file out, because it holds only throwaway files.

First, check where notes.txt stands. Run git log --oneline in your practice folder, then list the files in your latest commit with git ls-tree --name-only and that commit’s hash, as you saw in the last lesson:

Language: Shell
$ cd ~/grocery-practice
$ git log --oneline
30e2075 (HEAD -> main) Return cranberries to the regular price
9c48c0c Return eggs and cranberries to regular prices
d85bb1b Add practice notes and the sweet potato deal
3db0dc9 Add the practice price list
$ git ls-tree --name-only 30e2075
notes.txt
prices.js

Your hashes will be different from these, so use the one at the top of your own log. The latest snapshot holds both files, as expected.

Git keeps a file out of your history with a file called .gitignore. It’s a plain text file in the top folder of your project, and each line names a file that Git should ignore. Open your practice folder in your editor and create a new file named .gitignore, with the dot at the start. Give it one line:

Language: Text Filename: .gitignore
notes.txt

Save it. Like the .git folder, a file whose name starts with a dot is hidden on macOS and Linux, but your editor shows it in its Explorer panel. That’s one more reason to create it in the editor rather than in Finder or File Explorer.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.