Amending Commits
25.1 What Amending a Commit Means
Amending replaces the most recent commit with a new one. The commit hash changes, but the branch pointer moves forward as if the old commit never existed.
This is meant for local, unpublished commits only.
This is meant for local, unpublished commits only.
25.2 Amending the Commit Message
To change only the last commit message:
Git opens the editor to modify the message.
1git commit --amendGit opens the editor to modify the message.
25.3 Amending to Add Forgotten Files
If you forgot to include files in the last commit:
The new commit will include both old and new changes.
1git add <file>
2git commit --amendThe new commit will include both old and new changes.
25.4 Amending Without Changing the Message
To keep the existing commit message:
Useful when:
• Fixing typos
• Adding missing files
• Minor corrections
1git commit --amend --no-editUseful when:
• Fixing typos
• Adding missing files
• Minor corrections
25.5 Amending After Push (Why It's Dangerous)
Amending rewrites history. If the commit was already pushed:
• The remote history diverges
• Requires force push
• Can break teammates' work
Avoid amending pushed commits on shared branches.
• The remote history diverges
• Requires force push
• Can break teammates' work
Avoid amending pushed commits on shared branches.
25.6 Amending vs New Commit
• Use amend for small fixes to the latest commit
• Use a new commit for logical, standalone changes
Avoid amending pushed commits on shared branches.
• Use a new commit for logical, standalone changes
Avoid amending pushed commits on shared branches.
25.7 Key Takeaways
• git commit --amend replaces the latest commit.
• Amending changes the commit hash.
• Safe only for local, unpushed commits.
• --no-edit keeps the existing message.
• Never amend commits already shared with others.
• Amending changes the commit hash.
• Safe only for local, unpushed commits.
• --no-edit keeps the existing message.
• Never amend commits already shared with others.