如何撤消在 Git 提交消息中添加的换行符?

How do you undo your Add Line Break in Git Commit Message?

我知道如何在我的 git 提交消息中添加多行。

我正在使用:

git commit -m "
>Text here... 
>[Accidental Enter Key]

但是,如果我误按了回车键,想回到上一行怎么办?有这样的方法吗?

只需使用

git commit

它会为您的提交信息打开一个文本编辑器(通常是vim)。

Reference

GNU Readline 编辑有时很古怪。如果您不小心在 Bash 中的引号字符串中间按了 Enter,只需按 Ctrl-C。最后一个 Enter 之前的命令行部分保留在历史记录中。您只是丢失了最后一行:自上次 Enter 以来您键入的所有内容。在此 Ctrl-C 之后,您可以调用该行并对其进行编辑。这一次,您可以通过引号向后移动光标:您可以返回-space 不需要的多行部分以将它们连接成一行。

$ echo "abc
> oops hit Enter, didn't mean to
> oops, I did it again!^C

现在向上箭头:

$ echo "abc
oops hit Enter, didn't mean to_  <- cursor is here (lost the last line)
^
 `- no > characters here now, and you can move the cursor left
    to just before the "oops" and hit backspace to merge it with
    the "abc.

不清楚为什么 Readline 如此不一致:为什么有 > 继续该行的模式,但当您回想起来时可以进行正常编辑;也许有一种方法可以消除这种不友好的 > 愚蠢行为。我认为它的存在是为了新手:> 提示符是一个响亮而清晰的信号,指示 "hey, you have an unbalanced quote or something, and so I'm prompting you for more input"。不过,没有理由不允许用户至少返回 space 过去 >

无论如何,在这种情况下,您总是可以使用意外消息完成提交,然后立即执行

$ git commit --amend -m "corect message"   # oops
$ git commit --amend -m "correc message"   # darn
$ git commit --amend -m "correct message!"

最后,得到一个没有小后退space 键和正下方有一个巨大 Enter 键的键盘!那些愚蠢的键盘是您不小心按下 Enter: 的主要原因:正是在您想要返回 space 以修复错误的时候。想象一下,如果油门踏板就在刹车旁边!