CSC Git Guide

Ignore Specified Files

Ignore Specified Files

To tell Git to ignore certain files, create a .gitignore file in your project’s Git working directory.

GitRepositories  [a directory that contains one or more of your Git repositories]
   -> csc216-001-GP1-021 [a single Git repository]
      -> .git [may be a hidden folder, depending on your operating system]
      -> .gitignore

Conceptual Knowledge: Git Ignore Rules

The rules for .gitignore files include:

  • Blank lines, or lines starting with #, are ignored
  • Standard regular expression patterns work in .gitignore files
    • Asterisk (*) matches zero or more characters
    • [abc] matches any character inside the brackets
    • ? matches a single character
    • Brackets surrounding characters separated by a hyphen (for example, [0-9]) matches any character between those characters (in the example, any digit between 0 and 9, inclusive)
    • \*\* to match nested directories. For example, /projectName/\*\*/Main.java.
  • Start patterns with a forward slash (/) to avoid recursivity
  • End patterns with a forward slash (/) to indicate a directory
  • Negate a pattern by starting it with an exclamation point (!)

An example .gitignore file that you should use as a starting-point in this course:

# This rule ignores the contents of the bin/ build output directory (which includes .class files)
bin/

# This rule ignores temporary files that end with ~
*~

# This rule ignores the contents of the .settings directory generated by Eclipse
.settings/

Since .class files (and other build output) are generated when you build your actual .java files, you do not need to commit them to the repository. Similarly, sometimes (depending on your operating system and applications), temporary files may be generated that end with ~ (for example, when you are editing Microsoft Word files, Word creates a temporary document that ends with ~ until you save and close the document).

You can create your .gitignore file by either using Git Bash or a text editor.

Creating the .gitignore File using Git Bash

In Git Bash:

  1. Browse to your repository working directory.
  2. Execute the following command as a quick/shortcut way to generate your .gitignore file:

    touch .gitignore

The touch command will automatically create the .gitignore file for you.

  1. open the .gitignore file in a text editor, add your ignore rules, and save. Make sure the file does not somehow save with a .txt extension. We want the file to remain as .gitignore, not .gitignore.txt.

Creating the .gitignore File using a Text Editor

  1. Open a text editor (like Notepad, Notepad++, nano, or gedit).
  2. Add your ignore rules, and save the file as .gitignore. In the Save As… dialog, be sure to save the file type as (*.*) All Files, and not as (*.txt) Text Documents. If you save as a .txt file, your .gitignore will incorrectly be .gitignore.txt and will not work.