Step 1: Understand the commit history after commands
First commit message is "Add file". Then file.txt is updated and staged. The git commit --amend replaces the last commit message with "Add file with content update".
Step 2: Check the latest commit message output
The git log -1 --pretty=%B shows the last commit message, which is now "Add file with content update" after amend.
Final Answer:
Add file with content update -> Option B
Quick Check:
Amended commit message = updated message [OK]
Hint: Amend replaces last commit message and content [OK]
Common Mistakes:
Thinking amend adds a new commit instead of replacing
Expecting both messages to appear
Assuming commit message stays unchanged
4. You ran git commit --amend but accidentally removed some changes from the last commit. How can you fix this?
medium
A. Delete the repository and clone again.
B. Run git commit --amend again without changes.
C. Use git push --force to overwrite remote.
D. Use git reflog to find the previous commit and reset to it.
Solution
Step 1: Understand the problem with amend
Amending rewrites the last commit, so if changes were lost, the previous commit state is still in Git history.
Step 2: Use git reflog to recover
git reflog shows recent commit states. You can find the commit before amend and reset to it to restore lost changes.
Final Answer:
Use git reflog to find the previous commit and reset to it. -> Option D
Quick Check:
Recover lost commit with reflog [OK]
Hint: Use reflog to recover lost commits after amend [OK]
Common Mistakes:
Trying to fix by amending again without changes
Deleting repo instead of recovering
Forcing push without fixing local history
5. You committed a file with a typo in the message and forgot to add a new file. Which sequence correctly fixes both issues using amend?
hard
A. Add the new file, then run git commit --amend -m "Corrected message"
B. Run git commit --amend -m "Corrected message" first, then add the new file
C. Delete the last commit, add the new file, then commit again
D. Push the commit, then fix the message and add file in a new commit
Solution
Step 1: Stage the new file before amending
To include the new file in the last commit, you must add it first with git add.
Step 2: Amend the commit with the corrected message
Run git commit --amend -m "Corrected message" to update the commit message and include the staged new file.
Final Answer:
Add the new file, then run git commit --amend -m "Corrected message" -> Option A
Quick Check:
Stage files before amend to include them [OK]
Hint: Add files first, then amend commit message [OK]