2

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?

jakub.g
  • 38,512
  • 12
  • 92
  • 130
  • 1
    Does this answer your question? [how can I customize git's merge commit message?](https://stackoverflow.com/questions/3148863/how-can-i-customize-gits-merge-commit-message) – Dorin Botan Dec 05 '22 at 15:17
  • 1
    Not fully, I know how to customize the message (`git merge -m`), what I want is to customize && reuse the automated one. I updated my question, along with a 2-step workaround, but maybe there's a better solution. – jakub.g Dec 05 '22 at 16:49

1 Answers1

3

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

jakub.g
  • 38,512
  • 12
  • 92
  • 130
  • It seems to be either this, or assume something about the message that `git merge` would generate on its own. There is `git fmt-merge-msg` but it requires input that matches what `git fetch` leaves in `.git/FETCH_HEAD`, which is particularly annoying to generate here. – torek Dec 06 '22 at 01:39