Introduction

To ignore a class of files that you don’t want Git to automatically add or even show you as being untracked, create a named .gitignore and  add entries of those files as shown in following example.

$ cat .gitignore
*.[oa]
*~

First line show the content of file. Second line tells Git to ignore any files ending in “.o” or “.a”. Next line tells Git to ignore all files whose names end with a tilde (~).

Setting up a .gitignore file for your new repository is a good idea. Files with matching criteria will not be tracked by Git, so you don’t accidentally commit files that you really don’t want in your Git repository. Following are rules for the patterns that can be used in the .gitignore:

  • Blank lines or lines starting with # are ignored
  • Standard glob patterns work, and will be applied recursively throughout the entire working tree
  • Start patterns with a forward slash (/) to avoid recursively
  • End patterns with a forward slash (/) to specify a directory
  • Negate a pattern by starting it with an exclamation point (!)

Glob Pattern

Glob patterns are like simplified regular expressions that shells use. Example of glob pattern are

  • Asterisk (*) matches zero or more characters
  • [abc] matches any character inside the brackets (in this case a, b, or c)
  • Question mark (?) matches a single character
  • Brackets enclosing characters separated by a hyphen ([0-9]) matches any character between them (in this case 0 through 9)
  • Use two asterisks to match nested directories; a/**/z would match a/z, a/b/z, a/b/c/z, and so on

Example

Following example shows the content of a .gitignore file. Various pattern rules discussed above are explained with comment in this file.

# .gitignore file
# ignore all .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in any directory named build
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf

Troubleshooting

.gitignore only ignores files that are not part of the repository yet. If you already added files i.e. git add, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cached on them. Commit all changes, and then execute the following commands from the root folder of your repository.

git rm -r --cached .
git add .
git commit -m "Untracked files issue resolved"

Above example will untrack every file that is mentioned in .gitignore. git rm removes files from the working tree and the index, but not from your working directory. The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option. When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index. -r allows recursive removal when a leading directory name is given.

When you remove something from .gitignore file and the file does not appear to be tracked, you can add it manually as follows:

git add -f <file>
git commit -m "Re-Adding ignored file by force"

To remove specific files from the Git repository, use below command.

git rm --cached system/config.php

Above command removes a files named system/config.php. Now add this file to .gitignore, and it will not be tracked by Git.