How to amend a commit without changing commit message (reusing the previous one)?

226,314

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"
Share:
226,314
Sridhar Sarnobat
Author by

Sridhar Sarnobat

Updated on July 29, 2022

Comments

  • Sridhar Sarnobat
    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
      Sridhar Sarnobat over 9 years
      I'd downvote my own question after learning the hard way the evils of amending.
    • paullb
      paullb over 9 years
      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.
    • Ciprian Tomoiagă
      Ciprian Tomoiagă over 9 years
      Good article on amending: Thou shall not lie
    • Sridhar Sarnobat
      Sridhar Sarnobat over 6 years
      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).
    • DBCerigo
      DBCerigo over 6 years
      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)
    • Sridhar Sarnobat
      Sridhar Sarnobat over 6 years
      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
      Sridhar Sarnobat about 6 years
      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.
    • Lochlan
      Lochlan almost 6 years
  • mikej
    mikej about 12 years
    Just 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.
  • Dimitris Baltas
    Dimitris Baltas almost 12 years
    C'mon guys, don't be lazy, upgrade git and use the built-in command that Shaggle suggests! Plus one for -C option though.
  • Jherico
    Jherico about 11 years
    You can also make it easier to default to the --no-edit flag by adding an alias: "amend = commit -a --amend --no-edit"
  • user1338062
    user1338062 over 8 years
    Not only timestamp, but also the authorship information!
  • Sridhar Sarnobat
    Sridhar Sarnobat over 8 years
    Even 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
    Ruslan over 7 years
    ... <<< ZZ might be even less typing ;)
  • skwisgaar
    skwisgaar over 7 years
    ..and even less - ... <<< :x :)
  • B Seven
    B Seven over 7 years
    I don't think it is silly. It is a great way to improve the workflow for any command that opens up vi.
  • oligofren
    oligofren over 7 years
    triple angle brackets. that's new.
  • Ryan Castner
    Ryan Castner over 6 years
    When I use --no-edit the timestamp is still preserved, even though @RubenVerborgh said it wouldn't preserve it
  • Ruben Verborgh
    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
    leftaroundabout over 6 years
    I 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
    Sridhar Sarnobat over 6 years
    Actually 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
    Felix about 6 years
    I'd love to upvote this answer, but it does change (delete) the commit message.
  • Pat Myron
    Pat Myron over 5 years
    This will also work for Git versions prior to 1.7.9, unlike the accepted answer.
  • galva
    galva over 4 years
    It doesn't work with nvim for me, but does work with /usr/bin/vim on MacOS.
  • frouo
    frouo about 4 years
    @Jherico I would suggest removing -a. Please do atomic commits, it's way easier to review or rebase :)
  • lzap
    lzap over 3 years
    Why I am not using --no-edit for a decade now?!?! Thank you :-)
  • mfink
    mfink almost 3 years
    git config --global alias.amend 'commit --amend --no-edit'
  • chestozo
    chestozo over 2 years
    the only issue here - commit timestamp is reused. does anyone know how to have new commit timestamp + reuse last commit message?
  • detuur
    detuur almost 2 years
    What 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.