Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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

like image 579
Omer H Avatar asked Mar 17 '17 12:03

Omer H


People also ask

How do you git amend last commit?

Use git commit --amend to change your latest log message. Use git commit --amend to make modifications to the most recent commit. Use git rebase to combine commits and modify history of a branch.

Which option to git commit will modify last commit message?

You can change the most recent commit message using the git commit --amend command.

How do you amend the commit to change the commit message?

Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.

How do I add files to the last commit?

Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed. Enter git commit -m '<commit_message>' at the command line to commit new files/changes to the local repository.


1 Answers

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.
like image 122
Edward Thomson Avatar answered Oct 20 '22 14:10

Edward Thomson