Is there a way to amend a commit without vi
(or your $EDITOR
) popping up with the option to modify your commit message, but simply reusing the previous message?
766
votes
I'd downvote my own question after learning the hard way the evils of amending.
– Sridhar Sarnobat
As long as you abide by certain rules (like not amending something that is already pushed) there is no reason why amending has to be a bad thing.
– paullb
Amending commits should not be used for intermittent committing of work during a single logical change. For that you should commit locally properly and then squash the commit history once finished (@Sridhar-Sarnobat)
– DBCerigo
I completely agree @DBCerigo . The only situation I find amending useful is when I forgot to stage a file in a previous commit (eg because it is new and so doesn’t get auto staged when running git commit -a) and want to retroactively commit it.
– Sridhar Sarnobat
Another time amending is useful even if you recognize the dangers of changing the history is if you are unhappy with your most recent commit message and want to reword it without having to rebase.
– Sridhar Sarnobat
6 Answers
1185
votes
131
votes
78
votes
25
votes
16
votes
I use an alias that uses the accepted answer. Then this command can be used:
git oops
will add everything, and amend using the same message
git oops -m "new message"
will amend replacing the message
This is the alias
oops = "!f(){ \
git add -A; \
if [ \"$1\" == '' ]; then \
git commit --amend --no-edit; \
else \
git commit --amend \"$@\"; \
fi;\
}; f"
2
votes
just to add some clarity, you need to stage changes with git add
, then amend last commit:
git add /path/to/modified/files
git commit --amend --no-edit
This is especially useful for if you forgot to add some changes in last commit or when you want to add more changes without creating new commits by reusing the last commit.