在交互式rebase提交消息中转义#字符

Owa*_*ams 8 git

在git中执行交互式rebase后,我希望有一个以#(hash或pound)字符开头的提交消息,但以#开头的行#被视为注释并被忽略.

有没有办法逃脱#角色,使我的提交消息实际上以一个开头#

更多细节

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

git rebase -i HEAD~4
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

然后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
Run Code Online (Sandbox Code Playgroud)

但这意味着我的提交消息将被忽略.如何进行提交消息#91691 Implement log-in feature

Owa*_*ams 10

如果使用保存提交消息#,则会导致提交消息为空(您也可以删除提交消息中的所有内容).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
Run Code Online (Sandbox Code Playgroud)

所以,只需修改消息:

git commit --amend -m "#91691 Implement log-in feature"
Run Code Online (Sandbox Code Playgroud)

继续坚持:

git rebase --continue
Run Code Online (Sandbox Code Playgroud)

  • 如果提交非常大并且是多行怎么办,如何在行的开头添加哈希标记? (2认同)
  • 只需在关闭双引号之前添加换行符即可。Git 对多行、冗长的提交消息感到满意。 (2认同)
  • @Mohammed在交互式变基计划中,将提交保留为“pick”,但在要添加哈希值的提交之后添加“b”行。然后 `git commit --amend --cleanup=scissors` 和 `git rebase --continue`。 (2认同)