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?
-
9I'd downvote my own question after learning the hard way the evils of amending.– Sridhar SarnobatAug 11, 2014 at 6:35
-
49As 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.– paullbOct 20, 2014 at 9:48
-
7Amending 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)– DBCerigoJan 26, 2018 at 13:52
-
4I 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 SarnobatJan 26, 2018 at 23:38
-
1Another 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 SarnobatMar 30, 2018 at 23:36
6 Answers
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.
git commit -C HEAD --amend
will do what you want. The -C
option takes the metadata from another commit.
-
19Just 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.– mikejApr 19, 2012 at 21:35 -
11C'mon guys, don't be lazy, upgrade git and use the built-in command that Shaggle suggests! Plus one for -C option though. Jun 26, 2012 at 15:56
-
5
-
3@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. Aug 16, 2017 at 1:07 -
2Actually 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)”
Dec 7, 2017 at 1:30
Another (silly) possibility is to git commit --amend <<< :wq
if you've got vi(m) as $EDITOR
.
-
48Even 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. Jan 11, 2016 at 20:38
-
6
-
8
-
3I don't think it is silly. It is a great way to improve the workflow for any command that opens up vi.– B SevenDec 25, 2016 at 20:12
-
15
To extend on the accepted answer, you can also do:
git commit --amend --no-edit -a
to add the currently changed files.
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"
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.