How do I customize the format of git rebase --interactive commit messages?

泄露秘密 提交于 2021-02-06 16:24:24

问题


I use git for my local work (and love it ever so much), and I follow a workflow similar to the one described in this article. So basically, when starting on a new feature, I create a branch for it, go through the usual hack then commit cycle, and when I think I'm done with it, I squash it into a single commit using git rebase --interactive master, and I always end up editing the multitude of commit messages into something looking like the example in the article, reproduced here:

[#3275] User Can Add A Comment To a Post

* Adding Comment model, migrations, spec
* Adding Comment controller, helper, spec
* Adding Comment relationship with Post
* Comment belongs to a User
* Comment form on Post show page

Of course, that's after a bunch of removing # This is the xth commit message lines and copy/pasting * in front of each commit message.

Now, what I was wondering, is there any way to customize how git rebase -i outputs the squashed commit messages so I don't have to do all that hacking?

(I use msysgit, if that matters. My editor is Notepad++.)

Thanks!


回答1:


There's no way (short of hacking the source) to modify the squash message template, I don't think. However, you have a couple options:

  • Use a git log command to get the shortlist, something like `git log --pretty="* %s" commit-1..commit-2 to get you your bullets. In linux it's very possible to do this from within your editor - don't know how that works with msysgit.

  • Have your editor do the work for you! I don't know what your editor is, so I can't really tell you what to do, but it'd certainly be very possible in vim. (The idea being: search for /# This is the .* commit message/, delete a couple lines, keep one, delete up to the next comment)

Also, it's not what you want in this case, probably, but in fairly recent versions of git, there's a fixup identifier that you can use instead of squash - it does the same thing, but it discards the commit message, so if you have one commit with the real message then ten fixes, you can just mark them all fixup and not have to delete their throwaway messages.




回答2:


Starting Git 2.6+ (Q3 2015), there will actually be a way to configure git rebase -i commit message.

See commit 16cf51c (13 Jun 2015) by Michael Rappazzo (rappazzo).
(Merged by Junio C Hamano -- gitster -- in commit 9f56db7, 03 Aug 2015)

git-rebase--interactive.sh: add config option for custom instruction format

A config option 'rebase.instructionFormat' can override the default 'oneline' format of the rebase instruction list.

Since the list is parsed using the left, right or boundary mark plus the sha1, they are prepended to the instruction format.

You will soon have a new config:

rebase.instructionFormat

A format string, as specified in git log, to be used for the instruction list during an interactive rebase.
The format will automatically have the long commit hash prepended to the format.

For example:

git config --add rebase.instructionFormat "[%an @ %ar] %s"

Note there is a bug/regression after the release of that feature:
See "Comment in rebase instruction has become too rigid"

I noticed that the format of the comment lines in a rebase instruction sheet has become stricter - it could no longer begin with spaces or tabs. The comment char ("#" for example) has to appear on the first column.


Jefromi comments below:

it appears it's only meant to affect the display within interactive rebase, not the resulting commit messages.

I gave it a try with your example format string and I indeed saw the author information in my editor, but once I told it to squash, the resulting template commit message was still the usual one.

So this isn't a perfect fit for the OP.




回答3:


You can made a --amend when you want. You can checkout in commit previous you commit you want change and amend it.



来源:https://stackoverflow.com/questions/2480923/how-do-i-customize-the-format-of-git-rebase-interactive-commit-messages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!