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?

- 25,183
- 12
- 93
- 106
-
10I'd downvote my own question after learning the hard way the evils of amending. – Sridhar Sarnobat Aug 11 '14 at 06:35
-
64As 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 09:48
-
Good article on amending: [Thou shall not lie](http://paul.stadig.name/2010/12/thou-shalt-not-lie-git-rebase-ammend.html) – Ciprian Tomoiagă Nov 14 '14 at 13:12
-
It’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). – Sridhar Sarnobat Dec 09 '17 at 00:38
-
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) – DBCerigo Jan 26 '18 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 Sarnobat Jan 26 '18 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 Sarnobat Mar 30 '18 at 23:36
-
Possible duplicate of [How to skip the commit message step in "git commit --amend"?](https://stackoverflow.com/questions/5307417/how-to-skip-the-commit-message-step-in-git-commit-amend) – Lochlan Jul 10 '18 at 07:26
8 Answers
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.
-
62You 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
-
11@Jherico I would suggest removing -a. Please do atomic commits, it's way easier to review or rebase :) – frouo Apr 08 '20 at 10:54
-
18
-
3What 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 preserves `AuthorDate`, and updates `CommitDate`. – detuur Jun 21 '22 at 11:07
-
1Except --amend alone is actually a useful command to change the last commit message... – Ben Mar 13 '23 at 17:38
git commit -C HEAD --amend
will do what you want. The -C
option takes the metadata from another commit.

- 16,580
- 17
- 88
- 94

- 11,699
- 1
- 34
- 31
-
21Just 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 use `git amend` as a shortcut. – mikej Apr 19 '12 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. – Dimitris Baltas Jun 26 '12 at 15:56
-
5
-
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
-
4@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 01: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)”` – Sridhar Sarnobat Dec 07 '17 at 01:30
-
This will also work for Git versions prior to 1.7.9, unlike the accepted answer. – Pat Myron Nov 12 '18 at 18:13
-
the only issue here - commit timestamp is reused. does anyone know how to have new commit timestamp + reuse last commit message? – chestozo Nov 11 '21 at 12:17
-
Something else to consider with this method is that it re-runs the prepare-commit-msg hook. I have a hook that prepends a ticket number to my commit message, but if I run `-C HEAD`, then that ticket gets added again. --no-edit as above works as expected – Sinkingpoint Oct 04 '22 at 22:01
Another (silly) possibility is to git commit --amend <<< :wq
if you've got vi(m) as $EDITOR
.

- 1,253
- 10
- 17
-
51Even 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
-
9
-
9
-
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
-
17
-
1I 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. – leftaroundabout Sep 07 '17 at 10:52
-
1I'd love to upvote this answer, but it does change (delete) the commit message. – Felix Feb 21 '18 at 06:54
-
1It doesn't work with nvim for me, but does work with `/usr/bin/vim` on MacOS. – galva Sep 30 '19 at 10:05
To extend on the accepted answer, you can also do:
git commit --amend --no-edit -a
to add the currently changed files.

- 4,716
- 1
- 33
- 56
You can save an alias that uses the accepted answer so it can be used like this:
git oops
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"

- 7,879
- 3
- 33
- 28
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.

- 3,328
- 3
- 29
- 29
i see the question is already answered but if you want to add, commit and push like a efficient machine you can add all the commands together and do it in one line.
git add .; git commit --amend --no-edit; git push --force
Now when you are only doing small changes each commit you can just press UP
and do it all in one go.
hope this helps someone :)
Note: this is not a clean way of working with git and i would only recommend it if you are doing small changes often. For example always adding
with a .
(all changes) can lead to you staging changes that you don't even want to add wich can lead to a mess of a commit
.
Edit: You will need to add the --force
to your push command if the commit you want to amend to is already in the remote repository

- 765
- 3
- 14
-
1
-
1@Sagivb.g thx for the answer. Yes need a force when the previous commit you want to amend to is already in the remote repo. I will edit my answer based on the feedback – vince Jun 13 '23 at 12:59
Full Tutorial for Dummies
Once you finish your changes in the code.
1.- git status
to check in terminal
the changes;
2.- Save your changes by using git add .
or git add /your.file/
to do it file by file, using the previous command will help you in this last option;
3.- Once your changes have been staged you can now use git commit --amend --no-edit
. This will add your recent changes to your last commit without editing the message.
4.- Finally, if you didn't push your previous commit just make a git push
or git push origin/yourBranch
.
In case you already pushed your last commit use git push -f
or git push -f origin/yourBranch
.

- 163
- 1
- 9