Home > Software engineering >  Preserve automatic `git merge` message when passing custom message using `-m` option
Preserve automatic `git merge` message when passing custom message using `-m` option

Time:12-06

I use git merge --no-edit ... in a script.

When doing git merge --no-edit ..., git generates an automatic commit message like:

  • "Merge branch X into Y" or
  • "Merge commit X into Y" or
  • "Merge remote-tracking branch 'X' into Y".

It's possible to customize that message by doing git merge --no-edit -m "Custom message", but that overrides the automated one.

I'd like to have both: A custom message, but also keep the automated one appended to it.

Is there such a possibility? For example, via a placeholder like git merge -m "Custom message $AUTOMATIC_MESSAGE"? (is there such a magic placeholder?)

Edit 1:

One way to achieve what I want would be

  • Use git merge to create a merge with automated message
  • Then git commit --amend to edit the message in editor and preprend "Custom message"

However, I want the merge to be automated without a stop in editor.

Edit 2:

I realized I could proceed as follows in a script: do a merge with an default automatic message, then read that automated merge message post-factum via git log -1 --pretty=%s, then amend the message:

git merge ...
git commit --amend -m "Custom message: $(git log -1 --pretty=%s)"

However, is there a more efficient way by using just git merge without amending the commit?

CodePudding user response:

(Suboptimal but working self-answer)

It's possible to customize the merge message while preserving the original one by first doing a merge, then reading the automated commit message, then amending it.

git merge ...
ORIGINAL_MERGE_MESSAGE=$(git log -1 --pretty=%s)
git commit --amend -m "Custom message: ${ORIGINAL_MERGE_MESSAGE}"

(However I'd be happy to have a better solution).

CodePudding user response:

Yes, it is possible to include the automatically generated merge commit message in a custom commit message when you perform a git merge. To do this, you can use the %s placeholder in your custom commit message, which will be replaced with the automatically generated message when the merge is performed.

Here is an example of how you can use the %s placeholder in your custom commit message:

$ git merge -m "Custom message: %s"

When you run this command, git will generate the automatically generated merge commit message and insert it into your custom message, resulting in a commit message that looks something like this:

Custom message: Merge branch 'feature-x' into 'master'

You can also include the %s placeholder multiple times in your custom commit message, if you want to include the automatically generated message in multiple places. For example:

$ git merge -m "Custom message: %s. Additional information: %s"

This will result in a commit message that looks something like this:

Custom message: Merge branch 'feature-x' into 'master'. Additional information: Merge branch 'feature-x' into 'master'
  • Related