Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Git ignore files with .gitignore

After this lesson: I can keep files out of Git with a .gitignore file.

55 min lesson15 commandsGit and Gitea lab: learn clone, commit and push on a Git server of your own

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

WordWhat it meansExample
Ignoreto leave a file out of Git on purpose
Patterna line that names the files to ignore*.log
Trackedalready in a commit, so Git watches it
Untrackednot in any commit yet
Secrettext 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

PatternWhat it ignoresExample match
`key.txt`a file with exactly that namekey.txt
`*.log`any file ending in .logtill.log
`build/`a whole folderbuild/out.txt
`!keep.log`an exception: this one is not ignoredkeep.log

The cells students fill in are left blank in this preview.

Make a repository called site

Command
git init -b main site

Shown 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

Command
printf 'Latte 3.20\nTea 2.10\n' > site/prices.txt

Shown 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

Command
printf 'password=abc123\n' > site/key.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make a log file

Command
printf 'opened the till\n' > site/till.log

Shown 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

Command
git -C site add prices.txt key.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Commit them

Command
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

Command
printf '*.log\nkey.txt\n' > site/.gitignore

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

See what Git ignores

Command
git -C site status --short --ignored

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask which pattern matched

Command
git -C site check-ignore -v till.log

Shown 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

Command
printf 'password=xyz789\n' > site/key.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

See what Git says now

Command
git -C site status --short

Shown 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

Command
git -C site rm --cached key.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stage the ignore file

Command
git -C site add .gitignore

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Commit both changes

Command
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

Command
git -C site --no-pager log --format=%s -- key.txt

Shown 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.

About this lab and its source: Git and Gitea lab: learn clone, commit and push on a Git server of your own

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.