Skip to content

Ignoring things

Overview

Sometimes, you want to make sure that certain files such as temporary files or files containing sensitive data are not added accidently to your Git repository. Git allows you to define .gitignore files for this purpose.

Tips for instructors

Checkpoints:

  • Please make sure that the participants could commit their .gitignore and that no modifications exist in their repositories at the end of this episode.

Create some temporary files

First, we create some temporary files which we do not want in our repository:

  • mkdir results - Creates the directory results.
  • touch a.dat b.dat c.dat results/a.out results/b.out - Creates different temporary files in the results directory and in the root directory of our repository.
  • git status - Shows indicates three new files and one new directory.

In such a situation, you can quickly do mistakes and accidently add some of these files to your repository. For example, if you use git add ..

Add a .gitignore file

We can define which files we want to ignore by adding a .gitignore file:

  • touch .gitignore - Creates the file .gitignore which we can use to specify file name patterns of files we want to ignore. Please make sure that you do not misspell the file name because otherwise it will not work.
  • Edit the file .gitignore:
    • Please add the following content:
      *.dat
      results/
      
    • Please make sure that you added a closing blank line and saved the file properly.
  • In this example, we ignore all files with the file ending .dat and the directory results. You can use the * character to express an arbitrary number of characters. Every line can contain one file pattern. Git evaluates the file line by line from top to bottom.
  • git status - Indicates that our .gitignore definition works and only shows the new file .gitignore. The other files are no longer indicated as modifications in your repository.

Finally, we add the .gitignore file permanently to our repository:

  • git add .gitignore - Adds the .gitignore file to the staging area.
  • git commit -m "Ignore data files and the results folder" - Creates a new commit and adds the .gitignore file to your repository.
  • git status - Indicates no further modifications in the repository.

From now on, everyone who works with your repository already has the proper definition of ignored files.

Tips for working with .gitignore files

  • You can define multiple .gitignore files but we recommend to have one file in the root repository to avoid confusion.
  • You can use the ! character to negate a file pattern. For example, !*.dat means that Git should not ignore files with the file ending .dat.
  • git status --ignored shows all files that are currently ignored.
  • If a file is already committed to your repository, you cannot simply ignore it with a .gitignore pattern. Instead you have to remove the file from the repository first.
  • You can use git add -f <filename> or git add --force <filename> to add a file even if you ignored it. But only use it if you have a good reason and know what you are doing.
  • gitignore.io allows you to generate your specific .gitignore file. It provides sophisticated lists of patterns for a number of tools and programming languages. The resulting .gitignore file is licensed under CC0-1.0 and can be used freely.

Key points

  • You can use .gitignore files to avoid adding unwanted files to your Git repository.