.gitignore files


Code projects are required to have a .gitignore file in the root directory of the repository. The .gitignore file specifies which files and directories should be ignored by Git version control. This is important to prevent unnecessary files, such as build artifacts, temporary files, and sensitive information, from being tracked in the repository.

The following applies to all programming courses unless otherwise noted. Note that COSC 2325 Computer Organization / Machine Language and COSC 3301 Programming Languages do not count as programming courses for purposes of best practices and version control usage. Non-programming courses will have class specific guidelines provided by the instructor.

[ companion video ]


As a software developer, two of your responsibilities are:
  • ensure that your project repository remains clean and free from unnecessary files.
  • protect sensitive information from being exposed in public repositories.
There are two lines of defense to help you accomplish these tasks:
  • never use wildcards when using git add, always name the files you wish to add explicitly. Such as git add file.ext for one file or git add file1.ext file2.ext for multiple files.
  • the .gitignore file, which prevents certain files from being tracked by Git.

These two steps work together to help you maintain a clean and secure project repository. By never using wildcards with git add, you ensure that only the files you intend to include in your repository are added. Then the .gitignore file acts as a safeguard by specifying which files and directories can't be added. While both steps are subject to human error, using them in combination significantly reduces the likelihood of mistakes.

Tips for creating effective .gitignore files:

  • Use this guide to help you learn Git Ignore Files
  • To comment a .gitignore file, start the line with a hash symbol (#).
  • There are two basic approaches to creating .gitignore files:
    • Start by ignoring everything, and then unignore what you want to track.
    • Start by tracking everything, and then ignore what you don't want to track.