Git Ignore (.gitignore)

Not every file in your project should go in Git. Log files, passwords, installed packages and build output should stay out. The .gitignore file is a plain text file where you list the files and folders which Git must ignore.

Create a .gitignore file

Create the file in the root of your project, and write one pattern per line. Lines which start with # are comments.

# log files
*.log

# secrets
.env

# installed packages
node_modules/

# keep this one file
!keep.log

Now create some files and see what Git shows:

Terminal output

$ echo "error" > debug.log
$ echo "DB_PASSWORD=secret" > .env
$ mkdir node_modules
$ echo "x" > node_modules/lib.js
$ echo "<h1>Hi</h1>" > index.html
$ git status -s
?? .gitignore
?? index.html

Only .gitignore and index.html are shown. The log file, the .env file and the whole node_modules folder are ignored.

Pattern rules

PatternMeaning
*.logIgnore all files that end with .log
temp/Ignore the folder temp (a slash at the end means a folder)
/config.phpIgnore only config.php in the root, not in sub folders
doc/*.txtIgnore .txt files directly inside doc
doc/**/*.txtIgnore .txt files in doc and all its sub folders
!important.logDo not ignore this file (exception)
# commentA comment line

Check why a file is ignored

Terminal output

$ git check-ignore -v debug.log
.gitignore:2:*.log debug.log
$ git status –ignored -s
?? .gitignore
?? index.html
!! .env
!! debug.log
!! node_modules/

The first command tells the line of .gitignore which matched. This is very useful when a pattern does not work as you think.

Ignore a file which is already tracked

The .gitignore works only for untracked files. If a file was committed before, Git continues to track it, even if you add it to .gitignore later. You must remove it from the repository (not from your disk) with git rm --cached.

Terminal output

$ git add -A
$ git commit -q -m "Add website files"
$ echo "second error" >> debug.log
$ git status -s

No change is shown for debug.log, because it is ignored. But look what happens with a file which was committed first:

Terminal output

$ echo "<p>old</p>" > old.txt
$ git add old.txt
$ git commit -q -m "Add old.txt by mistake"
$ echo "old.txt" >> .gitignore
$ echo "<p>change</p>" >> old.txt
$ git status -s
M .gitignore
M old.txt
$ git rm –cached old.txt
rm 'old.txt'
$ git commit -q -am "Stop tracking old.txt"
$ echo "more" >> old.txt
$ git status -s

After the first git status, the file old.txt still shows as modified. After git rm --cached and a commit, Git forgets it and the file is ignored from then.

Global gitignore

For files which belong to your computer or editor (like .DS_Store, .idea/ or .vscode/), you can have one global ignore file for all your projects.

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

Common things to ignore

  • Dependencies: node_modules/, vendor/
  • Logs and temporary files: *.log, *.tmp
  • Secrets: .env, *.pem, config.local.php
  • Build output: dist/, build/
  • System and editor files: .DS_Store, Thumbs.db, .idea/
Note: If you committed a password by mistake, removing the file is not enough, because it stays in the history. Change the password immediately. Next: Git rm and mv.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.