your programing

명령 줄에서 'git commit -m'에 줄 바꿈 추가

lovepro 2020. 9. 30. 11:11
반응형

명령 줄에서 'git commit -m'에 줄 바꿈 추가


명령 줄에서 Git을 사용하고 git commit -m ""있으며 Vim에 들어 가지 않고 커밋 메시지에 줄 바꿈을 추가하려고합니다 (사용 ).

이것이 가능한가?


확실히, 그것이 어떻게 수행되는지는 당신의 셸에 달려 있습니다. Bash에서는 메시지 주위에 작은 따옴표를 사용할 수 있으며 따옴표를 열어두면 따옴표를 닫을 때까지 Bash가 다른 줄에 대한 프롬프트를 표시합니다. 이렇게 :

git commit -m 'Message

goes
here'

또는 "here 문서"(heredoc라고도 함)를 사용할 수 있습니다.

git commit -F- <<EOF
Message

goes
here
EOF

예를 들어 헤드 라인과 콘텐츠 라인 만 원하는 경우 다음을 사용할 수 있습니다.

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

Bash와 함께 명령 줄에서 Git을 사용하면 다음을 수행 할 수 있습니다.

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

Enter새 줄을 원할 때 입력하고 누르기 만하면 됩니다. ">"기호는를 눌렀 음을 의미 Enter하며 새 줄이 있음을 의미합니다 . 다른 답변도 작동합니다.


사용할 수 있어야합니다.

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

로부터 배쉬 설명서 :

$ ' string ' 형식의 단어는 특별히 처리됩니다. 이 단어 는 ANSI C 표준에 지정된대로 백 슬래시 이스케이프 문자가 대체 된 문자열 로 확장됩니다 .

여기에는 위에 표시된 줄 바꿈, 16 진수 및 유니 코드 코드 및 기타 지원이 포함됩니다. 백 슬래시 이스케이프 문자 목록을 보려면 링크 된 섹션으로 이동하십시오.


Git 커밋에 줄 바꿈 추가

여러 줄 커밋 메시지를 만들려면 다음을 시도하십시오.

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

다음과 같이 끝날 것입니다.

PowerShell의 여러 줄 Git 커밋 메시지

스크린 샷은 Poshgit과 함께 PowerShell을 사용하여 설정 한 예제에서 가져온 것입니다.


같은 일을

git commit -m"test\ntest"

작동하지 않지만 다음과 같은

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

works, but it's not very pretty. You set up a git-commitlb command in your PATH which does something like this:

#!/bin/bash

message=$1

git commit -m"$(echo -e "$message")"

And use it like this:

git commitlb "line1\nline2\nline3"

Word of warning, I have a feeling that the general convention is to have a summary line as the first line, and then two line breaks, and then an extended message in the commit message, so doing something like this would break that convention. You could of course do:

git commitlb "line1\n\nline2\nline3"

From Git documentation:

-m <msg>
--message=<msg>
Use the given <msg> as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

So, if you are looking for grouping multiple commit messages this should do the work:

git commit -m "commit message1" -m "commit message2"

I hope this isn't leading too far away from the posted question, but setting the default editor and then using

git commit -e

might be much more comfortable.


There is no need complicating the stuff. After the -m "text... the next line is gotten by pressing Enter. When Enter is pressed > appears. When you are done, just put " and press 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 ...

I use zsh on a Mac, and I can post multi-line commit messages within double quotes ("). Basically I keep typing and pressing return for new lines, but the message isn't sent to Git until I close the quotes and return.


In Bash/Zsh you can simply use literal line breaks inside quotes:

git commit -m 'Multi-line
commit
message'

ANSI-C quoting also works in Bash/Zsh:

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

You can also instruct Git to use an editor of your choice to edit the commit message. From the docs on git-commit:

The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order). See git-var for details.


If you are using Bash, hit C-x C-e (Ctrl+x Ctrl+e), and it will open the current command in your preferred editor.

You can change the preferred editor by tweaking VISUAL and EDITOR.

That's what I have in my .bashrc:

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

Personally, I find it easiest to modify commit messages after the fact in vi (or whatever your git editor of choice is) rather than on the command line, by doing git commit --amend right after git commit.


Here is a list of failing solutions on Windows with standard cmd.exe shell (to save you some trial-and-error time!):

  • git commit -m 'Hello Enter doesn't work: it won't ask for a new line

  • git commit -m "Hello Enter idem

  • git commit -m "Hello^ Enter idem

  • git commit -m 'Hello^ Enter World' looks like working because it asks "More?" and allows to write a new line, but finally when doing git log you will see that it's still a one-line message...

TL;DR: Even if on Windows, commandline parsing works differently, and ^ allows multiline input, it doesn't help here.

Finally git commit -e is probably the best option.


슬프게도 git은 메시지에 개행 문자를 허용하지 않는 것 같습니다. 위에 이미 여러 가지 합리적인 솔루션이 있지만 스크립팅 할 때는 성가시다. 여기 문서도 작동하지만 처리하기에는 너무 성 가실 수 있습니다 (yaml 파일을 생각해보십시오)

내가 한 일은 다음과 같습니다.

git commit \
    --message "Subject" \
    --message "First line$(echo)Second line$(echo)Third Line"

이것은 여전히 ​​추악하지만 여전히 유용 할 수있는 '한 줄'을 허용합니다. 일반적으로 문자열은 변수이거나 변수와 결합되기 때문에 uglynes는 최소한으로 유지 될 수 있습니다.

참고 URL : https://stackoverflow.com/questions/5064563/add-line-break-to-git-commit-m-from-the-command-line

반응형