Git Git Undoing Changes 2 β Questions and Answers
Question 1: How do you modify the message of the most recent commit without creating a new commit?
- git reset --soft HEAD~1 followed by git commit
- git commit --amend -m "new message" (Correct answer)
- git revert HEAD --message "new message"
- git log --amend
Correct answer: git commit --amend -m "new message"
`git commit --amend` replaces the most recent commit with a new one, allowing you to update the message or add more changes to it.
Question 2: After running `git commit --amend`, what happens to the original commit?
- It is automatically saved to a backup branch
- It remains accessible via git reflog until garbage collection runs (Correct answer)
- It is permanently and immediately deleted from all storage
- It becomes a parent commit of the new amended commit
Correct answer: It remains accessible via git reflog until garbage collection runs
The amended commit replaces the original with a new hash, but the original is still reachable through `git reflog` until Git's garbage collector removes unreachable objects.
Question 3: Which command allows you to find and recover the hash of a commit lost after `git reset --hard`?
- git log --all --oneline
- git reflog (Correct answer)
- git fsck --lost-found
- git stash list
Correct answer: git reflog
`git reflog` records a history of all HEAD movements including resets, letting you find the lost commit hash and restore it with `git reset --hard <hash>`.
Question 4: How long does Git keep reflog entries accessible by default before they expire?
- 7 days
- 30 days
- 60 days
- 90 days (Correct answer)
Correct answer: 90 days
By default, Git keeps reflog entries for 90 days, configurable via `gc.reflogExpire` in your Git config.
Question 5: Which flag with `git revert` prevents it from automatically creating a commit, letting you stage multiple reverts as a single commit?
- --soft
- --no-commit (Correct answer)
- --staged
- --hold
Correct answer: --no-commit
`git revert --no-commit` (or `-n`) applies the revert changes to the working tree and index without committing, so you can batch multiple reverts into one commit.
Question 6: What does `git clean -fd` do?
- Removes staged files and empty directories
- Removes untracked files and untracked directories (Correct answer)
- Force-deletes the last commit from history
- Cleans the reflog and triggers garbage collection
Correct answer: Removes untracked files and untracked directories
`git clean -fd` removes untracked files (`-f` for force) and untracked directories (`-d`), leaving only tracked content in the working directory.
Question 7: Which flag for `git clean` performs a dry run, showing what would be deleted without actually removing anything?
- --dry-run or -n (Correct answer)
- --preview or -p
- --list or -l
- --simulate or -s
Correct answer: --dry-run or -n
`git clean -n` or `git clean --dry-run` outputs what would be removed without actually deleting anything, letting you preview the impact first.
How do you modify the message of the most recent commit without creating a new commit?