首页 文章

从命令行向'git commit -m'添加换行符

提问于
浏览
577

我从命令行使用Git,我试图在提交消息中添加换行符(使用 git commit -m "" ),而不进入Vim .

这可能吗?

14 回答

  • 4

    当然,它是如何完成的取决于你的shell . 在Bash中,您可以在消息周围使用单引号,并且可以保持报价打开,这将使Bash提示另一行,直到您关闭报价 . 像这样:

    git commit -m 'Message
    goes
    here'
    

    或者,您可以使用“此处文档”:

    git commit -F- <<EOF
    Message
    goes
    here
    EOF
    
  • 0

    使用Bash命令行中的Git,您可以执行以下操作:

    git commit -m "this is
    > a line
    > with new lines
    > maybe"
    

    只需键入并在需要新行时按Enter键,">"符号表示您已按Enter键,并且有一个新行 . 其他答案也有效 .

  • 358

    如果你只想要一条头条和一条内容,你可以使用:

    git commit -m "My head line" -m "My content line."
    
  • 26

    你应该可以使用

    git commit -m $'first line\nsecond line'
    

    来自Bash manual

    $'string'形式的单词被特别处理 . 单词扩展为字符串,替换为ANSI C标准指定的反斜杠转义字符 .

    这包括对上述新行的支持,以及十六进制和Unicode代码等 . 转到链接部分以查看反斜杠转义字符的列表 .

  • 5

    Adding line breaks to your Git commit

    请尝试以下操作以创建多行提交消息:

    git commit -m "Demonstrate multi-line commit message in Powershell" -m "Add a title to your commit after -m enclosed in quotes,
    then add the body of your comment after a second -m.
    Press ENTER before closing the quotes to add a line break.
    Repeat as needed.
    Then close the quotes and hit ENTER twice to apply the commit."
    

    然后验证你做了什么:

    git log -1
    

    你应该得到这样的东西:

    Multi-line Git commit message in PowerShell

    截图来自我使用PowerShell和Poshgit设置的示例 .

  • 83

    做点什么

    git commit -m"test\ntest"
    

    不起作用,但有点像

    git commit -m"$(echo -e "test\ntest")"
    

    有效,但不是很漂亮 . 你在 PATH 中设置一个 git-commitlb 命令,它执行如下操作:

    #!/bin/bash
    
    message=$1
    
    git commit -m"$(echo -e "$message")"
    

    并像这样使用它:

    git commitlb "line1\nline2\nline3"
    

    警告的话,我有一种感觉,即一般惯例是将摘要行作为第一行,然后是两个换行符,然后是提交消息中的扩展消息,因此这样做会破坏该约定 . 你当然可以这样做:

    git commitlb "line1\n\nline2\nline3"
    
  • 512

    我希望这不会过于远离发布的问题,但是setting the default editor然后使用

    git commit -e
    

    可能会更舒服 .

  • 367

    来自Git documentation

    -m <msg> --message = <msg>使用给定的<msg>作为提交消息 . 如果给出多个-m选项,则它们的值将作为单独的段落连接 .

    所以,如果你正在寻找分组多个提交消息,这应该做的工作:

    git commit -m "commit message1" -m "commit message2"
    
  • 79

    我在Mac上使用zsh,我可以在双引号(“)中发布多行提交消息 . 基本上我一直在键入并按回新行,但是在我关闭引号并返回之前,消息不会发送到Git .

  • 39

    没有必要使这些东西复杂化 . 在 -m "text... 之后,按Enter键获取下一行 . 按Enter键时,出现 > . 完成后,只需输入 " 并按Enter键:

    $ git commit -m "Another way of demonstrating multicommit messages:
    >
    > This is a new line written
    > This is another new line written
    > This one is really awesome too and we can continue doing so till ..."
    
    $ git log -1
    commit 5474e383f2eda610be6211d8697ed1503400ee42 (HEAD -> test2)
    Author: ************** <*********@gmail.com>
    Date:   Mon Oct 9 13:30:26 2017 +0200
    
    Another way of demonstrating multicommit messages:
    
    This is a new line written
    This is another new line written
    This one is really awesome too and we can continue doing so till ...
    
  • 19

    如果您正在使用Bash,请按 C-x C-e (Ctrl x Ctrl e),它将在首选编辑器中打开当前命令 .

    您可以通过调整 VISUALEDITOR 来更改首选编辑器 .

    这就是我在_1453740中所拥有的:

    export ALTERNATE_EDITOR=''
    export EDITOR='emacsclient -t'
    export VISUAL='emacsclient -c'
    export SUDO_EDITOR='emacsclient -t'
    
  • 18

    在Bash / Zsh中,您只需在引号内使用文字换行符:

    git commit -m 'Multi-line
    commit
    message'
    

    ANSI-C quoting也适用于Bash / Zsh:

    git commit -m $'Multi-line\ncommit\nmessage'

    您还可以指示Git使用不同的编辑器 . 来自git-commit上的文档:

    用于编辑提交日志消息的编辑器将从GIT_EDITOR环境变量,core.editor配置变量,VISUAL环境变量或EDITOR环境变量(按此顺序)中选择 . 有关详细信息,请参阅git-var .

  • 5

    就个人而言,我发现最简单的方法是在 vi (或者你选择的任何git编辑器)之后修改提交消息,而不是在命令行上,通过在 git commit 之后执行 git commit --amend .

  • 7

    以下是Windows上带有标准cmd.exe shell的失败解决方案列表(为了节省一些试错时间!):

    • git commit -m 'Hello 输入don 't work: it won' t要求换行

    • git commit -m "Hello 输入同一性

    • git commit -m "Hello^ 输入同一性

    • git commit -m 'Hello^ 输入 World' 看起来像是在工作,因为它要求"More?"并允许写一个新行,但最后在做 git log 时你会发现它仍然是一行消息......

    TL; DR:即使在Windows上,命令行解析工作differently^ 允许多行输入,它也无济于事这里 .

    最后git commit -e可能是最好的选择 .

相关问题