Curriculum
Course: Git
Login
Text lesson

Git.gitignore

Git Ignore

When sharing your code, there are often files or parts of your project you might not want to include, such as:

  • Log files
  • Temporary files
  • Hidden files
  • Personal files
  • etc.

Git allows you to specify which files or parts of your project should be ignored by using a .gitignore file.

Git will not track the files and folders listed in .gitignore, but the .gitignore file itself is tracked by Git.

Create .gitignore

To create a .gitignore file, navigate to the root directory of your local Git repository and create the file there.

Example

[user@localhost] $

touch .gitignore

Now, open the file in a text editor.

We’ll add two simple rules:

  • Ignore all files with the .log extension.
  • Ignore everything in any directory named temp.

Example

# ignore ALL .log files
*.log

# ignore ALL files in ANY directory named temp
temp/

Now, all .log files and anything within temp folders will be ignored by Git.

Note: In this case, a single .gitignore file applies to the entire repository.

However, you can also have additional .gitignore files in subdirectories, which apply only to files or folders within those specific directories.

Rules for .gitignore

Here are the general rules for pattern matching in .gitignore files:

Pattern

Explanation/Matches

Examples

 

Blank lines are disregarded.

 

text comment

Lines beginning with # are ignored.

 

name

All files named name, all folders named name, and all files and folders within any folder named name.

/name.log
/name/file.txt
/lib/name.log

name/

A pattern ending with / specifies that it applies to a folder. It matches all files and folders within any folder named name.

/name/file.txt
/name/log/name.log

no match:
/name.log

name.file

All files with the name name.file.

/name.file
/lib/name.file

/name.file

A pattern starting with / specifies that it matches only files in the root folder.

/name.file

no match:
/lib/name.file

lib/name.file

Patterns specifying files in specific folders are always relative to the root directory, even if they don’t start with /.

/lib/name.file

no match:
name.file
/test/lib/name.file

**/lib/name.file

Starting with **/ before a / specifies that the pattern matches any folder in the repository, not just those at the root.

/lib/name.file
/test/lib/name.file

**/name

All folders named name, and all files and folders within any name folder.

/name/log.file
/lib/name/log.file
/name/lib/log.file

/lib/**/name

All name folders, and files and folders in any name folder within the lib folder.

/lib/name/log.file
/lib/test/name/log.file
/lib/test/ver1/name/log.file

no match:
/name/log.file

*.file

All files with the .file extension.

/name.file
/lib/name.file

*name/

All folders ending with name.

/lastname/log.file
/firstname/log.file

name?.file

? matches a single unspecified character.

/names.file
/name1.file

no match:
/names1.file

name[a-z].file

[range] matches a single character within the specified range (for example, any character from a to z, or any numeric character).

/names.file
/nameb.file

no match:
/name1.file

name[abc].file

[set] matches a single character from the specified set of characters (for example, a, b, or c).

/namea.file
/nameb.file

no match:
/names.file

name[!abc].file

[!set] matches a single character except those specified in the set (for example, any character except a, b, or c).

/names.file
/namex.file

no match:
/namesb.file

*.file

All files with the .file extension.

/name.file
/lib/name.file

name/
!name/secret.log

! specifies a negation or exception. It matches all files and folders in any name folder, except for name/secret.log.

/name/file.txt
/name/log/name.log

no match:
/name/secret.log

*.file
!name.file

! specifies a negation or exception. It matches all files with the .file extension, except for name.file.

/log.file
/lastname.file

no match:
/name.file

*.file
!name/*.file
junk.*

Adding new patterns after a negation will override a previously negated file. For example, all files with the .file extension are ignored, except for those in the name folder, unless the file is named junk.

/log.file
/name/log.file

no match:
/name/junk.file

Local and Personal Git Ignore Rules

You can also ignore files or folders without including them in the distributed .gitignore file.

These ignores are specified in the .git/info/exclude file, which functions like .gitignore but is not visible to others.