Some files must never be committed
A project folder holds more than code. It holds log files, copies a program makes and sometimes a password. A password committed once can be read by everyone who clones the project.
A file called .gitignore tells Git which files to leave alone. In this lesson you write one and fix a mistake.
By the end of this lesson
Note
I can keep files out of Git with a .gitignore file.
Key words
| Word | What it means | Example |
|---|---|---|
| Ignore | to leave a file out of Git on purpose | |
| Pattern | a line that names the files to ignore | *.log |
| Tracked | already in a commit, so Git watches it | |
| Untracked | not in any commit yet | |
| Secret | text such as a password that must stay private |
The cells students fill in are left blank in this preview.
A pattern names the files to ignore
Put a file called .gitignore in the project. Each line is a pattern. Git leaves matching files alone, so they stay untracked and never show up as new.
The dot at the start of the name is part of it. Commit the .gitignore itself, so everyone who clones the project gets the same rules.
Worked example: four kinds of pattern
| Pattern | What it ignores | Example match |
|---|---|---|
| `key.txt` | a file with exactly that name | key.txt |
| `*.log` | any file ending in .log | till.log |
| `build/` | a whole folder | build/out.txt |
| `!keep.log` | an exception: this one is not ignored | keep.log |
The cells students fill in are left blank in this preview.
Make a repository called site
git init -b main siteShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make a file with two prices
printf 'Latte 3.20\nTea 2.10\n' > site/prices.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make a file with a made-up password
printf 'password=abc123\n' > site/key.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make a log file
printf 'opened the till\n' > site/till.logShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Stage the prices and the key by mistake
git -C site add prices.txt key.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Commit them
git -C site commit -m "Add prices and key"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Write the ignore file
printf '*.log\nkey.txt\n' > site/.gitignoreShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
See what Git ignores
git -C site status --short --ignoredShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask which pattern matched
git -C site check-ignore -v till.logShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ignoring does not undo a commit
Git ignores only files it does not track yet. key.txt is already in a commit, so it is tracked, and the new pattern does nothing for it.
To stop tracking it, run git rm --cached. The option --cached removes the file from Git and keeps it in your folder. Without it the file is deleted from disk too.
Change the password in the file
printf 'password=xyz789\n' > site/key.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
See what Git says now
git -C site status --shortShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Stop tracking the key, keep the file
git -C site rm --cached key.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Stage the ignore file
git -C site add .gitignoreShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Commit both changes
git -C site commit -m "Ignore logs and stop tracking key.txt"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Find every commit that holds the key
git -C site --no-pager log --format=%s -- key.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think that ignoring a file or removing it from Git hides it for good. It does not. The first commit still holds the old password, so anyone with the history can read it. Treat a committed secret as leaked, and change the password.