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?
492
-
1duplicate: stackoverflow.com/questions/5307417/… – Max Nanasy Mar 6 '13 at 20:17
-
5I'd downvote my own question after learning the hard way the evils of amending. – Sridhar-Sarnobat Aug 11 '14 at 6:35
-
13As 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 Oct 20 '14 at 9:48
-
2Amending 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 Jan 26 '18 at 13:52
-
2I 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 Jan 26 '18 at 23:38
752
Since git 1.7.9 version 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 which may or may not be important to you.
-
30You can also make it easier to default to the --no-edit flag by adding an alias: "amend = commit -a --amend --no-edit" – Jherico Apr 22 '13 at 21:00
-
19You gotta love
git add README.md && git commit --amend --no-edit && git push -f
– Ciro Santilli 新疆改造中心 六四事件 法轮功 Feb 13 '14 at 10:44 -
I was hoping that this would work without
--amend
but it seems it does not :( – Sridhar-Sarnobat Aug 18 '15 at 0:58 -
What happens if you
git commit -a -m "some changes"
and then make more changes andgit commit -a -m "some changes"
? Committing back to back with same commit message. Would that be same as ammending? – oyalhi Dec 22 '15 at 17:48 -
7
97
git commit -C HEAD --amend
will do what you want. The -C option takes the metadata from another commit.
-
10Just 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. – mikej Apr 19 '12 at 21:35 -
7C'mon guys, don't be lazy, upgrade git and use the built-in command that Shaggle suggests! Plus one for -C option though. – Dimitris Baltas Jun 26 '12 at 15:56
-
3
-
When I use
--no-edit
the timestamp is still preserved, even though @RubenVerborgh said it wouldn't preserve it – Ryan Castner Aug 15 '17 at 13:36 -
2@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. – Ruben Verborgh Aug 16 '17 at 1:07
46
Another (silly) possibility is to git commit --amend <<< :wq
if you've got vi(m) as $EDITOR
.
-
28Even 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. – Sridhar-Sarnobat Jan 11 '16 at 20:38
-
4
-
2
-
3I don't think it is silly. It is a great way to improve the workflow for any command that opens up vi. – B Seven Dec 25 '16 at 20:12
-
8