Introduction
Every project produces files that do not belong in version control: compiled output, logs, dependency folders, editor settings, and above all secrets such as passwords and keys. A .gitignore file tells Git to leave them alone. At the other end of the workflow, GitHub Actions runs your tests, linters and builds automatically whenever someone pushes or opens a pull request, so problems are caught before they reach main.
How it works
.gitignore patterns, one per line (# starts a comment):
*.log: any file ending in .log, in any folder;*matches anything except a slash,?one character;build/: a trailing slash means directories only: everything inside any folder named build;/secret.txt: a leading (or middle) slash anchors the pattern to the repository root, so only the top-level secret.txt;docs/*.tmp: contains a slash, so it is anchored too: .tmp files directly in docs, not deeper;**/cache:**matches any number of folders;!keep.log: a!negates, re-including a path that an earlier pattern ignored.
Patterns are checked in order and the last match wins. One exception: if a whole directory is ignored, Git does not look inside it, so files in it cannot be re-included with !.
.gitignore only affects untracked files: a file already committed stays tracked until you git rm --cached it. git check-ignore -v path tells you which pattern ignores a path.
GitHub Actions workflows are YAML files in .github/workflows/:
name: tests
on:
push:
branches: [main, "release/**"]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: python -m pytest
on lists the triggering events; branch filters use glob patterns where * matches any characters except / and ** matches any characters including /. Each job runs on a fresh virtual machine and its steps run in order; a failing step fails the check, and branch protection can block merging until checks pass.
Example
import re
patterns = ["*.log", "build/", "/secret.txt", "!keep.log", "docs/*.tmp"]
paths = ["app.log", "keep.log", "src/build/out.o", "secret.txt", "src/secret.txt", "docs/a.tmp", "docs/deep/b.tmp", "main.py"]
def to_regex(pattern):
out = ""
for ch in pattern:
out += "[^/]*" if ch == "*" else "[^/]" if ch == "?" else re.escape(ch)
return re.compile(out + r"\Z")
def matches(pattern, path):
directory_only = pattern.endswith("/")
pattern = pattern.rstrip("/")
anchored = "/" in pattern
regex = to_regex(pattern.lstrip("/"))
parts = path.split("/")
candidates = ["/".join(parts[: i + 1]) for i in range(len(parts))] if anchored else parts
if directory_only:
candidates = candidates[:-1]
return any(regex.match(c) for c in candidates)
for path in paths:
ignored, rule = False, None
for pattern in patterns:
negate = pattern.startswith("!")
if matches(pattern.lstrip("!"), path):
ignored, rule = not negate, pattern
print(f"{path:18} {'ignored' if ignored else 'kept':8} {('(' + rule + ')') if rule else ''}")Line by line
- The patterns ignore logs, any build directory, the top-level secret.txt and .tmp files directly in docs, but re-include keep.log.
matchestreats a trailing slash as directories-only and a slash elsewhere as anchoring to the root.- Every pattern is tried in order, so the last one that matches decides.
- keep.log matches
*.logand then!keep.log, so it is kept; src/secret.txt is kept because/secret.txtis anchored. - docs/deep/b.tmp is kept because
*indocs/*.tmpdoes not cross a slash.
Expected result
app.log ignored (*.log)
keep.log kept (!keep.log)
src/build/out.o ignored (build/)
secret.txt ignored (/secret.txt)
src/secret.txt kept
docs/a.tmp ignored (docs/*.tmp)
docs/deep/b.tmp kept
main.py kept
In the real world
GitHub offers ready-made .gitignore templates for most languages. Accidentally committed secrets are one of the most common security incidents; GitHub's secret scanning looks for them, but the only real fix is to revoke the secret. Nearly every active GitHub project runs Actions to test pull requests, publish packages and deploy websites, often with a matrix of operating systems and language versions.
Common mistakes
- Adding a file to .gitignore after committing it and expecting Git to stop tracking it.
- Committing secrets and then deleting them in a later commit: they remain in history.
- Trying to re-include a file inside an ignored directory with
!. - Writing
*in a branch filter when**is needed to match names with slashes such as release/2.1.
Best practices
- Create a .gitignore at the start of every project, from a template.
- Keep secrets in environment variables or a secrets manager, never in the repository.
- Run the same checks locally and in CI, and require them to pass before merging.
Summary
.gitignore patterns keep unwanted files untracked: wildcards match within a folder, a trailing slash means directories, a slash anchors to the root, ! re-includes, and the last match wins. GitHub Actions workflows run jobs on events such as pushes and pull requests, filtered by branch patterns in which * stops at slashes and ** does not.
Quick check
Key takeaways
- In .gitignore, the last matching pattern decides; ! patterns re-include.
- A pattern without a slash matches in any folder; a trailing / matches only directories.
- Workflows run checks automatically on every push and pull request, often filtered by branch patterns.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.