How to amend a commit without changing commit message (reusing the previous one)?
Solution 1
Since Git 1.7.9
you can also use git commit --amend --no-edit
to get your result.
Note that this will not include metadata from the other commit such as the timestamp or tag, which may or may not be important to you.
Solution 2
git commit -C HEAD --amend
will do what you want. The -C
option takes the metadata from another commit.
Solution 3
Another (silly) possibility is to git commit --amend <<< :wq
if you've got vi(m) as $EDITOR
.
Solution 4
To extend on the accepted answer, you can also do:
git commit --amend --no-edit -a
to add the currently changed files.
Solution 5
You can save an alias that uses the accepted answer so it can be used like this:
git opps
adds everything, and amends using the same commit message
git oops -m "new message"
uses a new commit message.
This is the alias:
oops = "!f(){ \
git add -A; \
if [ \"$1\" == '' ]; then \
git commit --amend --no-edit; \
else \
git commit --amend \"$@\"; \
fi;\
}; f"
Sridhar Sarnobat
Updated on July 29, 2022Comments
-
Sridhar Sarnobat almost 2 years
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?-
Sridhar Sarnobat over 9 yearsI'd downvote my own question after learning the hard way the evils of amending.
-
paullb over 9 yearsAs 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.
-
Ciprian Tomoiagă over 9 yearsGood article on amending: Thou shall not lie
-
Sridhar Sarnobat over 6 yearsIt’s not so much that amending changes history for others that got me in trouble. The problem is if you keep amending, then when something finally goes wrong that you want to roll back, it might be hard to find the last good commit (I know you can use got reflog but if you’ve been switching back and forth between branches and your last good commit wasn’t recent, it can be tricky).
-
DBCerigo over 6 yearsAmending 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)
-
Sridhar Sarnobat over 6 yearsI 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 about 6 yearsAnother 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.
-
Lochlan almost 6 yearsPossible duplicate of How to skip the commit message step in "git commit --amend"?
-
-
mikej about 12 yearsJust to add to Andy's answer. If this is something you do frequently then you can set up an alias for it using
git config --global alias.amend 'commit --amend -C HEAD'
. You can then usegit amend
as a shortcut. -
Dimitris Baltas almost 12 yearsC'mon guys, don't be lazy, upgrade git and use the built-in command that Shaggle suggests! Plus one for -C option though.
-
Jherico about 11 yearsYou can also make it easier to default to the --no-edit flag by adding an alias: "amend = commit -a --amend --no-edit"
-
user1338062 over 8 yearsNot only timestamp, but also the authorship information!
-
Sridhar Sarnobat over 8 yearsEven if that's not necessary for this use case, I was unaware you can pipe to vim. That opens up some intriguing possibilities. Great tip.
-
Ruslan over 7 years
... <<< ZZ
might be even less typing ;) -
skwisgaar over 7 years..and even less -
... <<< :x
:) -
B Seven over 7 yearsI don't think it is silly. It is a great way to improve the workflow for any command that opens up vi.
-
oligofren over 7 yearstriple angle brackets. that's new.
-
Ryan Castner over 6 yearsWhen I use
--no-edit
the timestamp is still preserved, even though @RubenVerborgh said it wouldn't preserve it -
Ruben Verborgh over 6 years@RyanCastner Indeed, the comment you are referring to was from 2013. With the git version I have currently running,
--amend
, even without any other option, does preserve the author date (but changes the commit date). As such, I have removed my old comment. -
leftaroundabout over 6 yearsI think this is a hack that's horribly easy to get wrong (and Vims
Warning: Input is not from a terminal
rather reinforces that), but still very interesting that it works just like that. -
Sridhar Sarnobat over 6 yearsActually this answer is valuable in a different way even if it’s not the accepted answer. Unlike the other answer, you don’t have to use
—amend
. You can create a new commit but use the same message as the previous commit. That might not sound useful but my commit message by default when I’m just saving my work without having to think up a nice commit message, I keep reusing the message—message=“Work in progress (untested)”
-
Felix about 6 yearsI'd love to upvote this answer, but it does change (delete) the commit message.
-
Pat Myron over 5 yearsThis will also work for Git versions prior to 1.7.9, unlike the accepted answer.
-
galva over 4 yearsIt doesn't work with nvim for me, but does work with
/usr/bin/vim
on MacOS. -
frouo about 4 years@Jherico I would suggest removing -a. Please do atomic commits, it's way easier to review or rebase :)
-
lzap over 3 yearsWhy I am not using --no-edit for a decade now?!?! Thank you :-)
-
mfink almost 3 years
git config --global alias.amend 'commit --amend --no-edit'
-
chestozo over 2 yearsthe only issue here - commit timestamp is reused. does anyone know how to have new commit timestamp + reuse last commit message?
-
detuur almost 2 yearsWhat do you mean by "will not include metadata from the other commit"? As far as I can tell, it behaves identically as when running without
--no-edit
and then just saving the commit message. It preservesAuthorDate
, and updatesCommitDate
.