2Git, the concept


Ignoring files with .gitignore

Generally, there are files in a project that you want Git to ignore completely, typically these are files created by the operating system (.DS_Store on Macs, Thumbs.db on Windows &c.), temporary files, automatically generated log files &c.

With these sorts of files Git would normally flag them as untracked and would keep showing you them every time you did a status—eventually they would be accidentally added and committed to the local repository.

Git uses a file called .gitignore to tell it what files should be ignored completely. Such files are never mentioned in any status, never added to the staging area, or committed to the local repository.

The .gitignore file should be in the root of the working area of the project. In the lab-01-website we set up in § 2.2.1 it would be here:


Same place as the index.html file.

The .gitignore file is a simple text file (note it does not have an extension) that lists the files to ignore. It accepts single and multiple character wildcards (? and * respectively). It also accepts comments; anything after a # character to the end of the line is treated as a comment.

Here are some typical examples:

# # a comment everything after the # is treated as a comment
myfile.txt # ignore the specific file myfile.txt wherever it occurs
*.log # all files with a .log extension are ignored
/myfile.txt # ignore myfile.txt in the root, but not in any other folder or subfolder
mywork/ # ignore anything in the mywork folder
doc/*.txt # ignore any .txt file in the doc folder, but not in any other folder or subfolder
Code 2.8   gitignore typicals

Generally, both GitHub and Brackets-Git will create a .gitignore file by default when creating (initialising) a repository.

End flourish image