Fix the last commit's message and add a forgotten file with git commit --amend
You just committed, but the message has a typo and you forgot to stage one file. The commit is still local — you have not pushed it. Fold both fixes into a single tidy commit, without creating a second "fixup" commit on top.
Constraints: the commit must not be pushed yet; the forgotten file is config.yml; end with one corrected commit, not two.
git add config.yml
git ???
Complete the command, and say when amending is unsafe.
Stage the missed file, then git commit --amend -m 'fixed message'. Amend replaces the last commit, folding in the staged config.yml and the fixed message — it does not add a second commit. But amend rewrites that commit with a new hash, so once pushed and shared it forces a push --force that breaks others; only amend unpublished commits.
- ✗Amending a commit that has already been pushed and shared
- ✗Thinking amend adds a new commit rather than replacing the last one
- ✗Believing amend can only change the message, not the staged files
- →Why does amending an already-pushed commit force a
push --force? - →How is
git commit --amend --no-edituseful when you only add a file?
Solution
git commit --amend does not add a new commit; it replaces the last one: it takes whatever is currently staged plus a new message and rebuilds the commit with a new hash.
git add config.yml
git commit --amend -m "Fix typo in config loader"
the result is one tidy commit, not two.
amending needs push --force and breaks teammates' clones — only amend commits you have not published.
- The forgotten
config.ymlgoes into the same commit and the message is fixed — --no-editkeeps the existing message when you are only changing the contents.- The catch: amend rewrites the commit (new hash). Once it is pushed and shared,