This course will be delivered in blended learning mode (i.e., a mix of online and F2F activities) this semester.

Tools → Git and GitHub →

T2L6. Omitting Files from Revision Control


Git allows you to specify which files should be omitted from revision control.

This lesson covers that part.

You can specify which files Git should ignore from revision control. While you can always omit files from revision control simply by not staging them, having an 'ignore-list' is more convenient, especially if there are files inside the working folder that are not suitable for revision control (e.g., temporary log files) or files you want to prevent from accidentally including in a commit (files containing confidential information).

A repo-specific ignore-list of files can be specified in a .gitignore file, stored in the root of the repo folder.

The .gitignore file itself can be either revision controlled or ignored.

  • To version control it (the more common choice – which allows you to track how the .gitignore file changes over time), simply commit it as you would commit any other file.
  • To ignore it, simply add its name to the .gitignore file itself.

The .gitignore file supports file patterns e.g., adding temp/*.tmp to the .gitignore file prevents Git from tracking any .tmp files in the temp directory.

SIDEBAR: .gitignore File Syntax

  • Blank lines: Ignored and can be used for spacing.

  • Comments: Begin with # (lines starting with # are ignored).

     # This is a comment
    
  • Write the name or pattern of files/directories to ignore.

    log.txt          # Ignores a file named log.txt
    
  • Wildcards:

    • * matches any number of characters, except / (i.e., for matching a string within a single directory level):
      abc/*.tmp     # Ignores all .tmp files in abc directory
      
    • ** matches any number of characters (including /)
      **/foo.tmp    # Ignores all foo.tmp files in any directory
      
    • ? matches a single character
      config?.yml   # Ignores config1.yml, configA.yml, etc.
      
    • [abc] matches a single character (a, b, or c)
      file[123].txt # Ignores file1.txt, file2.txt, file3.txt
      
  • Directories:

    • Add a trailing / to match directories.
      logs/         # Ignores the logs directory
      
    • Patterns without / match files/folders recursively.
      *.bak         # Ignores all .bak files anywhere
      
    • Patterns with / are relative to the .gitignore location.
      /secret.txt   # Only ignores secret.txt in the root directory
      
  • Negation: Use ! at the start of a line to not ignore something.

    *.log           # Ignores all .log files
    !important.log  # Except important.log
    

Example:

.gitignore
# Ignore all log files
*.log

# Ignore node_modules folder
node_modules/

# Don’t ignore main.log
!main.log
HANDS-ON: Adding a file to the ignore-list

1 Add a file into your repo's working folder that you presumably do not want to revision-control e.g., a file named temp.txt. Observe how Git has detected the new file.
Add a few other files with .tmp extension.

2 Configure Git to ignore those files:

Create a file named .gitignore in the working directory root and add the text temp.txt into it.

echo "temp.txt" >> .gitignore
.gitignore
temp.txt

Observe how temp.txt is no longer detected as 'untracked' by running the git status command (but now it will detect the .gitignore file as 'untracked'.

Update the .gitignore file as follows:

.gitignore
temp.txt
*.tmp

Observe how .tmp files are no longer detected as 'untracked' by running the git status command.


The file should be currently listed under Unstaged files. Right-click it and choose Ignore.... Choose Ignore exact filename(s) and click OK.
Also take note of other options available e.g., Ignore all files with this extension etc. They may be useful in future.

Note how the temp.text is no longer listed under Unstaged files. Observe that a file named .gitignore has been created in the working directory root and has the following line in it. This new file is now listed under Unstaged files.

.gitignore
temp.txt

Right-click on any of the .tmp files you added, and choose Ignore... as you did previously. This time, choose the option Ignore files with this extension.

Note how .temp files are no longer shown as unstaged files, and the .gitignore file has been updated as given below:

.gitignore
temp.txt
*.tmp

3 Optionally, stage and commit the .gitignore file.

done!

Files recommended to be omitted from version control

  • Binary files generated when building your project e.g., *.class, *.jar, *.exe
    Reasons:
    1. no need to version control these files as they can be generated again from the source code
    2. Revision control systems are optimized for tracking text-based files, not binary files.
  • Temporary files e.g., log files generated while testing the product
  • Local files i.e., files specific to your own computer e.g., local settings of your IDE (.idea/)
  • Sensitive content i.e., files containing sensitive/personal information e.g., credential files, personal identification data (especially if there is a possibility of those files getting leaked via the revision control system).

EXERCISE: ignoring-somethings


DETOUR: Ignoring Previously-Tracked Files

Adding a file to the .gitignore file is not enough if the file was already being tracked by Git in previous commits. In such cases, you need to do both of the following:

  1. Untrack the file (i.e., remove the file from the staging area and stop tracking it in future), using the git rm --cached <file(s)> command.
    git rm --cached data/ic.txt
    
  2. Add it to the .gitignore file, as usual.