Unit 7: Git - gitignore

Table of Contents

  1. .gitignore
  2. Globs
  3. When to use a .gitignore
  4. Further Reading

.gitignore

If you want Git to not track certain files or directories, you can store them in a .gitignore file (a text file). All you have to do is make the file in the root directory of your project (name it .gitignore) and add files and/or directories to the text file. Make sure to commit the .gitignore file. From now on, any file/directory on the gitignore will be ignored by Git, so if you modify it, it will not show up.

Note: Any file that used to be tracked by Git will still remain in the repository’s remote. To remove a file from Git but keep it in your working directory, see git rm.

Note: If you want to write comments on the .gitignore file, use a #.

For example, this is what the .gitignore file from the Learn Code repository looks like:

# Ignore .DS_Store files
*.DS_Store

# Ignore IntelliJ IDEA files
.idea/
HowToCode.iml

# Ignore metadata generated by Jekyll
_site/
.sass-cache/
.jekyll-cache/
.jekyll-metadata

# Ignore folders generated by Bundler
.bundle/
vendor/

Notice that directories are signified with a /.

Globs

You can also use globs to specify file patterns instead of specific file or directory names. For example, *.java would ignore all files that have the .java extension. To learn more about globs, see the Atlassian tutorial in Further Reading.

When to use a .gitignore

You should use a .gitignore file to ignore things like personal configuration files created by your IDE or Text Editor. For example, Visual Studio Code often has workspace settings stored in a JSON file and Intellij IDEA stores configuration files in the .idea directory of a project.

Further Reading

You can read about the .gitignore file on the Atlassian tutorial or GitHub tutorial.

Here is a repository of common .gitignore templates.