With git rebase, is there a way to reword commit messages in the git-rebase-todo using the default commands?

[亡魂溺海] 提交于 2019-12-05 13:32:39

问题


Suppose I run git rebase -i HEAD~3

pick 6b24464 foo
pick a681432 Foo
pick 8ccba08 foo foo

# Rebase 960c384..8ccba08 onto 960c384
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

I would like to reword commit messages directly from this file, rather than editing each commit message one at a time (as per usual with reword), like so:

reword 6b24464 foo bar
reword a681432 Foo Bar
reword 8ccba08 foo foo bar bar

# Rebase 960c384..8ccba08 onto 960c384
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

I had hoped that this could be done using just reword or edit, but I can't seem to figure out how (if even possible). I've been able to achieve the same result with

x git cherry-pick 6b24464 && git commit -amend "foo bar"

However, this is more time consuming than I'd like for large rebases. Any ideas?


回答1:


I do not believe that can be done, because when you do a git rebase -i you get only the first line of each commit message, not the full commit message.

As a convention, a commit message should have the first line be only a summary, and have more details about the change in the following lines. Here's Linus Torvalds on how to write a proper commit message

Also, please write good git commit messages. A good commit message looks like this:

Header line: explain the commit in one line (use the imperative)

Body of commit message is a few lines of text, explaining things
in more detail, possibly giving some background about the issue
being fixed, etc etc.

The body of the commit message can be several paragraphs, and
please do proper word-wrap and keep columns shorter than about
74 characters or so. That way "git log" will show things
nicely even when it's indented.

Make sure you explain your solution and why you're doing what you're
doing, as opposed to describing what you're doing. Reviewers and your
future self can read the patch, but might not understand why a
particular solution was implemented.

Reported-by: whoever-reported-it
Signed-off-by: Your Name <youremail@yourhost.com>

where that header line really should be meaningful, and really should be just one line. That header line is what is shown by tools like gitk and shortlog, and should summarize the change in one readable line of text, independently of the longer explanation. Please use verbs in the imperative in the commit message, as in Fix bug that..., Add file/feature ..., or Make Subsurface...

So, even though you may have done only one line of message per commit, this is not what git is assuming, so it won't let you edit a commit message through a "one line per commit view".




回答2:


You can probably use the exec option to achieve what you want.

Use an exec command similar to git commit --amend -m "this is the new message"

You can either use that directly, or if you need more sophistication put it in an external script called my_reword.

#!/bin/sh
git commit --amend -m "$1"

Then add in the exec line wherever you want

pick 6b24464 foo
pick a681432 Foo
x my_reword "this is a new message"
pick 8ccba08 foo foo



回答3:


No, you can't reword messages in the rebase TODO file.

The reason for this: Git is applying the rebase from top to bottom. The TODO file is only telling Git in what order to perform a rebase operation, and what its action is (squash, reword, use, fixup, etc). It then relies on other mechanisms (like commit) to handle the rest.

If you have a lot of commits to reword, you're going to get a lot of prompts to edit the message, which is what you should be expecting; you're creating a new commit [regardless] with a new message, so Git should be asking you to enter it in while it's in the middle of processing the rebase operation.

I'd advise strongly against large rebase operations as it leads to this sort of mundane thing, and it has the risk of being more dangerous than it's worth.




回答4:


added to my .gitconfig:

[alias]
    w = "!reword() { git commit --amend -m \"$1\"; }; reword"

Then, x git w "..." just like Andrew C proposed in his answer.

As far as I know, people also usually name the alias ca...



来源:https://stackoverflow.com/questions/29198289/with-git-rebase-is-there-a-way-to-reword-commit-messages-in-the-git-rebase-todo

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