The .gitignore file

What you do not version (compiled output, dependencies, secrets, editor files) and the syntax of .gitignore patterns: star, slash, double star, exclamation mark, and the case of an already tracked file.

  • 20 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • The .gitignore file: what is it?

    Show the answer

    A text file, versioned with the project, that lists name patterns Git must leave untracked.

    It only has an effect on files not yet tracked. Since it is versioned, the whole team shares the same rules.

  • .gitignore contains *.class, /build/ and !Main.class. Which statement is right?

    • src/Main.class is not ignored, build/Main.class is
    • build/Main.class is no longer ignored because of !Main.class
    • src/Main.class is ignored, a ! only works at the root
    • No .class is ignored, the ! cancels *.class
    Show the answer

    src/Main.class is not ignored, build/Main.class is

    !Main.class re-includes Main.class files outside build. But in build/, the whole folder is ignored, and Git does not look inside: nothing can be re-included there.

  • Files the project can …, such as compiled binaries or downloaded dependencies, are not versioned.

    Show the answer

    Files the project can regenerate, such as compiled binaries or downloaded dependencies, are not versioned.

    They weigh down the repository and create conflicts at every build. The source code and the dependency file are enough to reproduce them.

  • Pattern with a star (*): what is it?

    Show the answer

    Stands for any run of characters in a file name, never crossing a folder separator.

    *.class ignores every .class file, but docs/*.pdf only reaches the files directly in docs, not those in subfolders.

  • You add .env to .gitignore, but git status still shows it as modified. What do you do so it is finally ignored, while keeping it on your disk?

    • git rm .env then a commit
    • git add .gitignore then a commit
    • git restore .env so it becomes untracked again
    • git rm --cached .env then a commit
    Show the answer

    git rm --cached .env then a commit

    A .gitignore only concerns untracked files; .env was already tracked. --cached removes it from the index without deleting it, whereas git rm .env would delete it from the disk too, and committing the .gitignore changes nothing for a tracked file. Careful: its content stays in the history, so a secret has to be changed.