【问题标题】:Escape # character in interactive rebase commit message [duplicate]在交互式 rebase 提交消息中转义 # 字符 [重复]
【发布时间】:2016-05-03 04:15:30
【问题描述】:

在 git 中执行交互式变基后,我想要一个以 #(哈希或井号)字符开头的提交消息,但以 # 开头的行被视为 cmets 并被忽略。

有什么方法可以转义# 字符,使我的提交消息实际上以# 开头?

更多详情

我正在使用以下方式执行交互式变基:

git rebase -i HEAD~4

然后,在编辑器中我正在做任何需要的事情,例如:

pick b010299 #91691 Add test for logging in with valid credentials
reword 5e9159d 91691 Implement log-in feature
pick 2735aa3 #91691 Re-factor logic
pick 14bd500 #91691 Tidy up 'using' declarations

# Rebase 60d6e3f..14bd500 onto 60d6e3f
#
# 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

然后 git 在我的文本编辑器中为我想要改写的提交加载提交消息,但我想以 # 开头来保存我的提交消息:

#91691 Implement log-in feature

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 60d6e3f
# You are currently editing a commit while rebasing branch 'master' on '60d6e3f'.
#
# Changes to be committed:
#   modified:   My.Website.LogInController.cs

但这意味着我的提交信息将被忽略。如何制作提交消息#91691 Implement log-in feature

【问题讨论】:

  • 它并不漂亮,但它比公认的答案更快:在“#”前面添加一个空格。因为只有以字符开始的行被忽略。
  • 只有在它有效时才会更快 ;-) 我一开始就想要哈希的原因是因为我的问题跟踪集成正在寻找以哈希开头的提交。我想这是可能如果有前导空格它会找到它们,但文档表明它需要以哈希开头。

标签: git


【解决方案1】:

如果您使用# 保存您的提交消息,它会导致您的提交消息为空白(您也可以删除提交消息中的所有内容)。 Git 会告诉你该怎么做:

Aborting commit due to empty commit message.
Could not amend commit after successfully picking 5e9159d9ce3a5c3c87a4fb7932fda4e53c7891db... 91691 Implement log-in feature
This is most likely due to an empty commit message, or the pre-commit hook
failed. If the pre-commit hook failed, you may need to resolve the issue before
you are able to reword the commit.
You can amend the commit now, with

        git commit --amend

Once you are satisfied with your changes, run

        git rebase --continue

所以,只需修改消息:

git commit --amend -m "#91691 Implement log-in feature"

并继续变基:

git rebase --continue

【讨论】:

  • 如果commit很大而且多行怎么办,如何在行首添加hashmark?
  • 在关闭双引号之前插入换行符。 Git 对多行、冗长的提交消息感到满意。
猜你喜欢
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 2017-08-19
  • 2017-10-27
  • 2010-12-16
相关资源
最近更新 更多