Gitignore Patterns
Part of the Version Control section of Coddy's Terminal journey — lesson 20 of 58.
The patterns inside .gitignore follow a few simple rules:
secret.txt: ignore an exact filename anywhere in the repo*.log: ignore every file ending in.logbuild/: ignore the entirebuilddirectory and everything inside it
logs/*.tmp: ignore.tmpfiles insidelogs/only#: starts a comment line; ignored by Git!important.log: an exception. Even if a previous rule matched it, this file is not ignored
Order matters when you mix rules and exceptions. Later patterns override earlier ones for files they both match.
One important warning: .gitignore only affects files that are not yet tracked. If a file was already committed, adding it to .gitignore will not stop Git from tracking it. You would need to remove it from the index first.
Challenge
EasyThe folder contains: main.py, secret.txt, tmp/cache.dat, and tmp/keep.md. Initialize the repo, create a .gitignore with these two lines:
secret.txt
tmp/Then run git status --short. Only .gitignore and main.py should show up as untracked.
Cheat sheet
.gitignore pattern rules:
secret.txt— ignore exact filename anywhere in the repo*.log— ignore all files ending in.logbuild/— ignore entire directory and its contentslogs/*.tmp— ignore.tmpfiles insidelogs/only#comment— comment line, ignored by Git!important.log— exception: do not ignore this file
Later patterns override earlier ones. .gitignore only affects untracked files; already-committed files must be removed from the index first.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Version Control
2Getting Started
Initialize A RepositoryThe .git FolderConfigure Your IdentityGit StatusRecap - First Repo8Merging
What Is A MergeFast-Forward MergeThree-Way MergeMerge ConflictsResolve A ConflictRecap - Merge Master11Feature Branch Project
Project OverviewInitialize Main3Tracking Changes
The Staging AreaGit AddGit CommitModifying A Tracked FileGit LogRecap - First Commits6Recipe Site Project
Project OverviewInitialize And Ignore