Omer H
Omer H

Reputation: 435

How to automatically `git commit amend` to *append* to last commit message?

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

Upvotes: 16

Views: 7102

Answers (5)

samtax01
samtax01

Reputation: 952

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.

Upvotes: 1

Samir Aguiar
Samir Aguiar

Reputation: 2559

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>.

Upvotes: 8

Abizern
Abizern

Reputation: 150605

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."

Upvotes: 2

qonf
qonf

Reputation: 1091

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"

Upvotes: 2

Edward Thomson
Edward Thomson

Reputation: 78673

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 <[email protected]>
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 <[email protected]>
Date:   Fri Mar 17 12:29:10 2017 +0000

    This is the commit message.

    This is appended.

Upvotes: 28

Related Questions