Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

A Basic .gitignore

Create the file in the repository root:

# secrets and local settings
.env

# dependencies
vendor/
node_modules/

# logs and caches
*.log
storage/cache/

Then commit it like any other file:

git add .gitignore
git commit -m "Add .gitignore"

Lines starting with # are comments. Blank lines are ignored.

Pattern Rules

PatternMatches
*.logAny file ending in .log, in any folder
/config.phpOnly config.php in the root
build/Folders named build, anywhere
logs/*.log.log files directly in logs
**/cachecache at any depth
debug?.txtdebug1.txt, debugA.txt

A pattern with a slash at the start or in the middle is relative to the folder of the .gitignore file. A pattern without one matches at any depth. A trailing slash matches folders only. You can also place a .gitignore in a subfolder; its patterns apply to that folder.

Negation with !

A line starting with ! re-includes something an earlier line excluded:

*.log
!install.log

Git cannot re-include a file whose parent folder is excluded, because it never looks inside that folder. To keep one file in an otherwise ignored folder, ignore the folder's contents rather than the folder:

uploads/*
!uploads/.gitkeep

Stop Tracking a File Already Committed

Adding a committed file to .gitignore changes nothing: Git keeps tracking it. Check what is tracked, then remove the file from the index while keeping it on disk:

git status
git rm --cached .env
git commit -m "Stop tracking .env"

For a folder, use git rm -r --cached storage/cache. Before you push, know two things:

  • Other checkouts: when another clone or server pulls this commit, Git deletes the file there. Copy it aside on those machines first.
  • History: the file stays in every earlier commit. If it held passwords or keys, change them; untracking does not make them secret again.

A Global Ignore File

Files your editor or operating system creates belong in a personal ignore file, not in every project:

git config --global core.excludesFile ~/.gitignore_global

Put patterns such as *.swp or .DS_Store in that file. If you do not set the option, Git reads ~/.config/git/ignore when it exists (or the same file under $XDG_CONFIG_HOME when that variable is set). For patterns that apply to one clone only and should not be committed, use .git/info/exclude.

Find Out Why a File Is Ignored

git check-ignore -v storage/cache/page.html

The output names the ignore file, the line number, and the pattern that matched. No output means the file is not ignored. To list the ignored files and folders in the project:

git status --ignored

Common Mistakes

  • Rules in a subfolder: patterns in a subfolder's .gitignore are relative to that subfolder, not to the repository root.
  • Wrong file name: the file is .gitignore, with the leading dot.
  • Ignoring the lock file: files such as composer.lock should usually be committed, so every server installs the same versions.

Ignored files are also left alone by git stash unless you add -a. To set Git up on a new server, see how to install Git on Linux; to undo a commit that added the wrong file, see git reset explained.

Was this answer helpful?
Loading...