Git Git History and Log 3 β Questions and Answers
Question 1: How do you display the number of insertions and deletions per file in each commit?
- git log --diff-stat
- git log --stat (Correct answer)
- git log --changes
- git log --files
Correct answer: git log --stat
The `--stat` option adds a summary of file changes (insertions/deletions) below each commit entry in the log.
Question 2: What does `git log --follow <file>` do compared to `git log <file>`?
- Follows symlinks to the real file
- Tracks the file's history across renames (Correct answer)
- Shows only commits where the file was created
- Limits history to the last rename
Correct answer: Tracks the file's history across renames
The `--follow` flag instructs Git to continue the log history across file renames, showing the full lineage of a file.
Question 3: Which `git log` option searches for commits that added or removed a specific string in the code (the 'pickaxe')?
- --grep
- -S (Correct answer)
- -G
- --content
Correct answer: -S
The `-S` option (pickaxe search) finds commits that changed the number of occurrences of the given string in the codebase.
Question 4: How do you view the full diff of a specific commit by its hash?
- git log <hash>
- git show <hash> (Correct answer)
- git diff <hash>
- git view <hash>
Correct answer: git show <hash>
The `git show <hash>` command displays the commit metadata and the full diff introduced by that commit.
Question 5: What does the three-dot syntax `git log main...feature` show?
- Commits in both branches
- Commits reachable from either branch but not their common ancestor (Correct answer)
- Commits only in main
- All commits since the repository was created
Correct answer: Commits reachable from either branch but not their common ancestor
The triple-dot syntax shows commits that are reachable from either `main` or `feature` but not from their common merge base.
Question 6: Which option in `git log` excludes merge commits from the output?
- --no-merges (Correct answer)
- --exclude-merges
- --skip-merges
- --without-merges
Correct answer: --no-merges
The `--no-merges` flag filters out any commit that has more than one parent, effectively hiding merge commits.
How do you display the number of insertions and deletions per file in each commit?