15

Trying to edit last commit message by appending a string in the end of the previous message.

Trying to do this from a CI server, so I'm looking for an automatic solution that doesn't require any interactive human intervention

Omer H
  • 415
  • 5
  • 14
  • how CI server will know that a wrong msg has been commited and a new message needs to be given, without manual intervention? – Anil Mar 17 '17 at 12:29
  • I'm using an external library with a certain flag that only commits a message and doesn't push it intentionally. I need to append a string to it for important integration with the CI server that is triggered based on certain commit messages parameters. – Omer H Mar 17 '17 at 13:55

5 Answers5

23

If you want to amend the current commit and change the commit message without opening an editor, you can use the -m flag to supply a message. For example:

git commit --amend -m"This is the commit message."

Will amend the current HEAD, using the message given as the new message:

% git log
commit adecdcf09517fc4b709fc95ad4a83621a3381a45
Author: Edward Thomson <ethomson@edwardthomson.com>
Date:   Fri Mar 17 12:29:10 2017 +0000

    This is the commit message.

If you want to append the message, you'll need to fetch the previous one:

OLD_MSG=$(git log --format=%B -n1)

And then you can use that to set the new message, using multiple -m's to set multiple lines:

git commit --amend -m"$OLD_MSG" -m"This is appended."

Which will append the given message to the original:

% git log
commit 5888ef05e73787f1f1d06e8f0f943199a76b70fd
Author: Edward Thomson <ethomson@edwardthomson.com>
Date:   Fri Mar 17 12:29:10 2017 +0000

    This is the commit message.

    This is appended.
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • 1
    Excellent! Thank you – Omer H Mar 17 '17 at 14:35
  • how would you go about aliasing this? (appending to last commit message) – Stuart Nov 28 '18 at 16:40
  • any ideas for not adding the newlines? – jayqui Apr 22 '20 at 22:10
  • @jayqui I don't know which newlines you're talking about precisely. If you want to just remove all newlines from the old message, then you can use any command line tool. `xargs` wasn't made to do this, but can. For example: `OLD_MSG=$(git log --format=%B -n1 | xargs)` – Edward Thomson Apr 23 '20 at 09:32
7

One way is to use the GIT_EDITOR environment variable and amend the commit:

GIT_EDITOR="echo 'appended line' >>" git commit --amend

where appended line is the content you want to insert.

This variable will set the editor to be used for this commit operation. Basically Git will execute whatever that variable is set to, passing a file name containing the commit message, i.e. $GIT_EDITOR <file> or, in this case, echo 'appended line' >> <file>.

Samir Aguiar
  • 2,509
  • 2
  • 18
  • 32
3

This is the one line that I use all the time to amend the last commit

git commit --amend -C head

the -C means "Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit."

If you want to make other changes use -c instead

the -c means "Like -C, but with -c the editor is invoked, so that the user can further edit the commit message."

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • For interactive usage this is superior to the accepted answer (but the question was about automation). Just replace `head` with `HEAD` – twil Jul 10 '23 at 09:30
  • `HEAD` may be necessary for some systems, but on macOS, `head` works just fine. – Abizern Jul 11 '23 at 14:30
2

Combine git log with git commit --amend:

APPEND_MESSAGE="foo bar baz"
PREVIOUS_MESSAGE="$(git log --format=%B -n 1)"
git commit --amend -m "$PREVIOUS_MESSAGE" -m "$APPEND_MESSAGE"
qonf
  • 1,051
  • 1
  • 10
  • 21
0

A great way to achieve this in one line would be

git commit --amend -m "$(git log --format=%B -n1)" -m "This is appended message"

Where the last path("This is appended message") is the additional message.

samtax01
  • 834
  • 9
  • 11