CH.08 · 12 MIN · PART 2

See what changed. Take it back. Hide what does not belong.

Three small tools you will use every day: git diff, git restore, and .gitignore.

git diffgit restore.gitignore
Added and removed lines beside an ignore gate and restore arrowCHANGES- old line+ new lineIGNORE.envdiff / restore / ignore

Diff: what changed?

red out, green in

git diff shows the exact lines you changed since the last add or commit. Lines starting with - were removed, + were added.

@@ todo.txt @@ buy milk - call mom + call mom and dad

.gitignore tester

edit the rules

A file called .gitignore lists things Git should never track: secrets, build output, huge folders. A line starting with ! makes an exception. Edit the rules and watch which files are ignored.

Ignoring does not untrack. A rule in .gitignore only affects files Git is not tracking yet. If a file was already committed, Git keeps tracking it and keeps showing its changes. Add the rule before the first commit, or stop tracking with git rm --cached file. If a secret was already committed, ignoring it is not enough: treat it as leaked (Part 3 covers this). You will try it in the practice below.

When it goes wrong

real messages, plain meaning
error: pathspec 'todo.txt' did not match any file(s) known to gitGit has never seen that fileCheck the spelling with ls. If the file is new, use git add first.
The following paths are ignored by one of your .gitignore filesA rule is hiding that fileFix the rule, or add -f if you truly want it tracked.
(empty output from git diff)Nothing to show, or it is stagedgit diff only shows unstaged changes. Try git diff --staged.

Practice: diff then restore

hands-on

Make a change, look at it, then throw it away safely.

1

Loading

practice terminal · bash-style

Quick check

1 / 3

Pick an answer.
Git, Visually · Parts 1 and 2 · English