Git Alias for Amend Without Editing Message
Create a git alias to amend the last commit without changing its commit message. Perfect for adding forgotten files or fixing small mistakes.
Detailed Explanation
Amend Without Editing the Message
The amend alias is one of the most useful workflow shortcuts. It adds staged changes to the previous commit without opening the editor to change the message:
[alias]
amend = commit --amend --no-edit
When to Use It
This alias is perfect for the "oops, I forgot a file" moment. The typical workflow is:
git commit -m "Add user validation"
# Realize you forgot to stage a file
git add src/validators/user.ts
git amend
The forgotten file is now included in the original commit with the same message.
What Happens Under the Hood
git commit --amend replaces the tip of the current branch with a new commit that combines the previous commit's changes with any newly staged changes. The --no-edit flag reuses the existing commit message without opening an editor.
Important Caveat
Never amend commits that have been pushed to a shared branch. Amending rewrites history by creating a new commit hash. If others have already based work on the original commit, amending will cause divergence and force-push complications.
Safe to amend:
- Local commits not yet pushed
- Commits on your personal feature branch (with force-push)
Unsafe to amend:
- Commits on main/develop that others have pulled
- Any commit in a shared branch without team agreement
Amend with Message Change
If you also want to fix the commit message:
[alias]
reword = commit --amend
This opens your editor to modify the message while keeping all the same file changes.
Use Case
Use this alias multiple times daily when you realize you forgot to stage a file, want to add a minor fix to the last commit, or need to include a formatting change. It keeps your commit history clean by avoiding 'fix typo' follow-up commits.