Git & GitHub Interview Question #56
How do you amend the most recent Git commit?
Undo & Recovery, Tags, Forks, Pull Requests & Code Review Mid-Level Intermediate
Quick Interview Answer
git commit --amend replaces the most recent commit with a new commit. It can be used to include newly staged changes or correct the latest commit message.
Detailed Explanation
git commit --amend replaces the most recent commit with a new commit. It can be used to include newly staged changes or correct the latest commit message.
Because amending changes the commit ID, it rewrites history. It is straightforward for a local unpublished commit, but amending a commit already shared with others requires coordination.
Code Example
# Add a forgotten file
git add README.md
git commit --amend --no-edit
# Change the latest message
git commit --amend -m "Correct commit message"