3

I have setup path to the template file on my config file, and it works fine when I do basic commit (git add -A . && git commit):

[commit]
        template = .git-commit-template.txt

Instead when I try to squash N-commits, and also add their messages, my commit template does not show up.

=> Is there any way add git template message during squashing last N commits?


The way I squash my last 2 commits with the help of this answer: https://stackoverflow.com/a/5201642/2402577

If you want to start editing the new commit message with a concatenation of the existing commit messages:
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

#!/bin/bash

git reset --soft HEAD~2
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})" # template message does not show up

As alternative when I do following I get the same result:

git reset --hard HEAD~2 && git merge --squash HEAD@{1} && git commit

alper
  • 2,919
  • 9
  • 53
  • 102
  • I think you’re pre-populating the message to edit—where would your template even go? – D. Ben Knoble Apr 18 '20 at 13:58
  • You don’t see the template because you are specifying that you want the output from `git log --format=%B --reverse HEAD..HEAD@{1}` instead. – matt Apr 18 '20 at 13:59
  • Even if I do `git commit` template does not show up @matt – alper Apr 18 '20 at 14:19
  • You said “it works fine when I do basic commit”. There is no difference between making a commit before and after a reset. It is still “a basic commit” (whatever that means). – matt Apr 18 '20 at 14:25
  • Sorry if I do: `git reset --hard HEAD~2 && git merge --squash HEAD@{1} && git commit` in order to edit into previous git commit message along the way. @matt – alper Apr 18 '20 at 14:28

2 Answers2

1

I was looking for something as follows, where I need to print the template message using $(cat .git_commit_template.txt):

Updated code:

git reset --soft HEAD~2
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1}) $(cat .git_commit_template.txt)"
git push -f
alper
  • 2,919
  • 9
  • 53
  • 102
0

The -m argument you have in your sample command overrides your template.

You might want to write a small script that:

  • reads your template;
  • runs the desired git log command;
  • inserts the git log output at some defined template location (perhaps using a syntax of your own invention in the template);
  • writes the resulting file somewhere, as a temporary file;
  • invokes git commit --edit -F /path/to/temporary/file rather than git commit --edit -m ...;
  • removes the temporary file when git commit itself returns;
  • exits with the exit status of the git commit command.

Note, however, that git merge --squash obeys the merge.log configuration knob, which might be close to what you want. You can run git -c merge.log=20 merge --squash .... The drawback is that this is the %s format, not the %B format: you get only the subject lines.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Do I get the subject lines on the git commit section? @torek – alper Apr 19 '20 at 11:54
  • Try it out and see. – torek Apr 19 '20 at 12:10
  • I did `git -c merge.log=20 merge --squash HEAD@{1}` but still having the same output – alper Apr 19 '20 at 12:24
  • You do have to run `git commit` after `git merge --squash`, but when I do that, I actually get a full `git log` of the included commits (not just subject lines as documented). This is with Git 2.24.0, but except for doing a full `git log` rather than just subject lines, this should be the same all the way back to old Git 1.x. – torek Apr 19 '20 at 20:50
  • I do get `git log` results as well but in addition it does not add the git template message, do you able to get the template message? @torek – alper Apr 20 '20 at 11:45
  • I don't use a template. It's not clear how a template would interact with the use of `merge.log`, since Git does not have templates-with-formatting-directives. – torek Apr 20 '20 at 18:33
  • I wanted to see template during the commit of the merge, where I edit my commit message @torek – alper Apr 20 '20 at 19:16
  • 1
    A template is a plain text file. It has nothing in it other than the plain text you put in it. You *must* choose: do you want a plain text file with no log in it, or do you want a log? You cannot have both. I also showed you a method by which you can *write a program* that *combines* a template of your choice (in a format of your choice) with `git log` output to produce a *new* plain text file that you can use as a single-use template. **You must write this program yourself.** – torek Apr 20 '20 at 19:19
  • Adding `$(cat .git_commit_template.txt)` solved my problem – alper Apr 21 '20 at 22:29
  • 1
    That's a nice simple program. :-) It just sticks your template after (or before) the log messages, depending on where you put it. It's still a *program:* a little bit of shell script. This is why one should learn shell scripting. – torek Apr 22 '20 at 01:39