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.
Which of the following statements is true about a .gitignore file placed in a repository's root directory?
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.
After committing a file to a git repository, what happens if you add its name to .gitignore?
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.
If your working directory has several untracked files and you run git clean -f, what is the effect?
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.
Given the line /logs/ in the root .gitignore, which of these files will be ignored?
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.
How does git display files that are listed in .gitignore but exist in your working directory when running git status?
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.