Having Git Ignore Files

Why Should Files Be Ignored

Remember a couple sections back when we were learning aboutgit add? Instead of adding the files one by one, there was a special character that we could use to indicate the current directory and all subdirectories. Do you remember what that character is?

That's right, the period (.)!

The Problem

Let's say you add a file like a Word document to the directory where your project is storedbut don't want it added to the repository. (You can simulate adding a Word document by runningtouch project.docx) Git will see this new file, so if you rungit statusit'll show up in the list of files.

The Terminal application showing the output of thegit statuscommand. The output shows a new Word document that is in Git's "Untracked files" section.

The potential problem with having this file in your project is that, becausegit add .adds_all_files, the Word document might get accidentally committed to the repository.

Git Ignore

If you want to keep a file in your project's directory structure but make sure it isn't accidentally committed to the project, you can use the specially named file,.gitignore(note the dot at the front, it's important!). Add this file to your project in the same directory that the hidden.gitdirectory is located. All you have to do is list the_names_of files that you want Git to ignore (not track) and it will ignore them.

Let's try it with the "project.docx" file. Add the following line inside the.gitignorefile:

project.docx

Now rungit statusand check its output:

The Terminal application showing the output ofgit status. The Word document is no longer listed as an untracked file. The new ".gitignore" file is listed, though.

Git knows to look at the contents of a file with the name.gitignore. Since it saw "project.docx" in it, it ignored that file and doesn't show it in the output ofgit status.

Globbing Crash Course

Let's say that you add 50 images to your project, but want Git to ignore all of them. Does this mean you have to list each and every filename in the.gitignorefile? Oh gosh no, that would be crazy! Instead, you can use a concept calledglobbing.

Globbing lets you use special characters to match patterns/characters. In the.gitignorefile, you can use the following:

  • blank lines can be used for spacing
  • # - marks line as a comment
  • * - matches 0 or more characters
  • ? - matches 1 character
  • [abc] - matches a, b, _or _c
  • ** - matches nested directories - a/**/z matches
    • a/z
    • a/b/z
    • a/b/c/z

So if all of the 50 images are JPEG images in the "samples" folder, we could add the following line to.gitignoreto have Git ignore all 50 images.

samples/*.jpg

QUESTION 1 OF 2

Which of the following files will be ignored if*.pngis entered into the.gitignorefile?

  • ocean.jpg

  • trees.png

  • png-format.pdf

  • not-a-png.jpeg

  • bg-pattern.png

  • logo.gif

  • LOUDFILE.PNG

SUBMIT

QUESTION 2 OF 2

If you ask Git to ignore "be?rs", which of the following filenames will be ignored?

  • bears

  • beavers

  • BeArS

  • beers

  • boars

SUBMIT

Git Ignore Recap

To recap, the.gitignorefile is used to tell Git about the files that Git should not track. This file should be placed in the same directory that the.gitdirectory is in.

Further Research

results matching ""

    No results matching ""