Understanding .gitignore and Maintaining Clean Git Working Trees Quiz

This quiz explores how to effectively use .gitignore files and keep git working trees clean. Designed to deepen your understanding of ignoring files, managing untracked changes, and related git concepts, it's ideal for users looking to optimize their version control workflow.

  1. .gitignore File Scope

    Which of the following statements is true about a .gitignore file placed in a repository's root directory?

    1. It ignores specified files and directories relative to its own location and subdirectories.
    2. It prevents tracked files from being modified.
    3. It ignores all files globally, regardless of location.
    4. It only works if placed inside a subdirectory.

    Explanation: .gitignore files apply to the directory in which they are located, affecting specified files and subdirectories beneath them. They do not prevent tracked files from being modified; they only affect untracked files. To ignore files globally, a global gitignore must be configured, not just a local one. Placing .gitignore in a subdirectory only applies it to that directory and its subfolders, not the entire project.

  2. Effect of .gitignore on Tracked Files

    After committing a file to a git repository, what happens if you add its name to .gitignore?

    1. The file remains tracked and changes are still staged and committed.
    2. The file is automatically deleted from the repository.
    3. Git renames the file as ignored_file.
    4. The file becomes untracked and is ignored in all future operations.

    Explanation: .gitignore only affects files that are untracked; already tracked files remain tracked even if listed in .gitignore. They will continue to be included in staging and commits unless manually removed. Git does not rename files as a result of .gitignore, nor does it automatically delete them. Simply listing a file in .gitignore does not untrack it.

  3. Purpose of git clean Command

    If your working directory has several untracked files and you run git clean -f, what is the effect?

    1. All untracked files in the working directory will be deleted.
    2. All changes to tracked files will be permanently staged.
    3. Untracked files will be renamed rather than deleted.
    4. The working directory is reset but ignored files are kept.

    Explanation: The git clean -f command deletes all untracked files from the working directory, helping maintain a tidy workspace. It does not stage changes to tracked files or rename untracked files. The reset of the working directory only affects untracked content and may preserve files ignored by .gitignore if used with certain options but is not the default behavior described here.

  4. Ignoring Patterns in .gitignore

    Given the line /logs/ in the root .gitignore, which of these files will be ignored?

    1. logs/error.log in the root directory
    2. app/logs/error.log in a subdirectory
    3. error.log at the project root
    4. logs.txt in the root directory

    Explanation: The pattern /logs/ in .gitignore matches the logs directory at the project root and all of its contents, so files like logs/error.log will be ignored. However, it does not match directories named logs that appear in subdirectories (like app/logs/error.log), nor files like error.log or logs.txt at the root. The match is specific to the logs folder at the root due to the leading slash.

  5. Untracked vs. Ignored Files

    How does git display files that are listed in .gitignore but exist in your working directory when running git status?

    1. They are not shown unless requested with a specific flag.
    2. They are always shown under 'Untracked files'.
    3. They are displayed as 'Modified files'.
    4. They show up under 'Changes to be committed'.

    Explanation: By default, git status hides ignored files, and you won't see them unless you use flags like --ignored or -uall. Files in .gitignore are not shown as untracked or modified unless explicitly requested. Only untracked files not ignored are shown under 'Untracked files', and ignored files are never staged or displayed as 'Changes to be committed' automatically.