Git without fear · Chapter 4 of 28
Ignore the junk
By the end of this chapter you can keep secrets, downloads and logs out of Git, and stop tracking a file that should never have been committed. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Day one
- 4 tasks
- Git
- In the all labs plan
The first 2 chapters of Git without fear are free. This one comes with the all labs plan.
Why you need this
Bunty added a tip to the order total in orders.js, then did what he always does, git add ., and went for chai. git status shows what he staged:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: .env
new file: node_modules/left-pad/index.js
new file: node_modules/left-pad/package.json
modified: orders.js
.env holds the payments key (a fake one, luckily). node_modules/ is downloaded code, thousands of files in a real project. Neither belongs in Git. And yesterday Bunty committed debug.log, which changes every time the server runs.
With what you know so far, you could stage files one by one forever, and the next git add . would sweep the junk in again. Committing now would put the key into the history, where everyone who clones the repository gets it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Commit the tip, without the junk
Bunty ran git add . and staged junk with his real change. Commit only the real change.
1. Run git status. .env, node_modules and orders.js are staged.
2. Take .env and node_modules out of the staging area. Do not delete them from the folder.
3. Commit orders.js, with a message about the tip.
To pass: the latest commit's orders.js has the withTip function. No commit on any branch has .env or a file in node_modules.
- Task 2
Commit a .gitignore for the junk
Tell Git to ignore the junk, so nobody commits it by mistake again.
1. Make a file called .gitignore in the chapter folder.
2. Write three patterns in it, one on each line: the node_modules/ folder, the .env file, and every file that ends in .log.
3. Run git status. The junk is no longer listed.
4. Stage .gitignore and commit it.
To pass: .gitignore is in your latest commit. It ignores node_modules/, .env and every .log file (not only debug.log). It does not ignore orders.js or menu.txt.
- Task 3
Stop tracking debug.log, but keep the file
debug.log was committed yesterday, so .gitignore does not work for it. Stop tracking it, but keep the file.
1. Use git rm with the option that removes a file from the staging area only.
2. Run git status. debug.log shows as deleted, to be committed.
3. Commit that, for example together with your .gitignore.
To pass: debug.log is not staged and not in your latest commit. debug.log, .env and node_modules all stay in the chapter folder.
- Task 4 · interview question
Interview: stop tracking a file, but keep it
Interview question: which command stops Git tracking a file, but keeps the file in the folder?
1. It is the command you used on debug.log in task 3.
2. Save the git command and its option in answer.txt, in the chapter folder. A file name after it is fine.
To pass: answer.txt has the full git command with its option, written out.
What you will learn
- git restore --staged: take files out of the staging area, and keep them on disk.
- .gitignore: a file of patterns Git should never offer to track.
- git rm --cached: stop tracking a file that is already committed, and keep it on disk.
More in Day one
Build "Ignore the junk" yourself
Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Git without fear are free. This one comes with the all labs plan.