1

On my Mac, I submit git information through the following command. I want git information to display newlines, so I added \n to the string. But it doesn't seem to work, and git won't wrap lines according to \n symbols:

commit_log1="111\n"
commit_log2="222"
git commit -m "${commit_log1}${commit_log2}" 

enter image description here

Does anyone know how to make git wrap lines according to the symbols in the string

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
maofu
  • 163
  • 2
  • 12
  • You can give multiple `-m` arguments to `git commit`. Like `git commit -m "${commit_log1}" -m "${commit_log2}"` The first one will be the subject. Next ones will constitute the body. – Romain Valeri Sep 22 '22 at 12:59
  • That is the output in a visual git viewer, what does `git log` display? – Randommm Sep 22 '22 at 13:06
  • what shell is used to execute the commands you mention ? – LeGEC Sep 22 '22 at 14:34
  • You can type (by keys) `git commit -m "first line` (without closing `"`), then press return to have your shell open a new line, and once you're done finish it up with another `"` + return – Torge Rosendahl Sep 22 '22 at 14:53

2 Answers2

1

This is very probably a shell issue, not a git issue :

# zsh :
$ echo "foo\nbar"
foo
bar

# bash :
$ echo "foo\nbar"
foo\nbar

If you ran the commands you show in your question with bash (perhaps from a script starting with #!/bin/bash ?) you will have a litteral \n instead of a newline in your git message.


If you want to go with bash, you can choose one of many ways to add newlines to your commit message :

(bash) using the $'...' syntax (see this question) :

# bash :
$ echo $'foo\nbar'
foo
bar
$ commit_log1=$'111\n'
$ commit_log2="222"
$ git commit -m "${commit_log1}${commit_log2}"

(bash) plain old newlines in a string litteral :

$ commit_log1="111"
$ commit_log2="222"
$ git commit -m "${commit_log1}
${commit_log2}"

(git) using git, read commit message from stdin or from a file :

# from stdin
$ (echo 111; echo 222) | git commit -F -

# from a file
$ git commit -F /tmp/mycommitmessage

(git) provide several times the -m option :

$ git commit -m "111" -m "222"

...

LeGEC
  • 46,477
  • 5
  • 57
  • 104
1

This is working for me:

git commit -m "$( echo -e $commit_log1$commit_log2 )"

In bash, at least.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • My environment is bash, and my submission information is uncertain, so it needs to be put in variables. This method is more suitable for my application scenarios. Thanks for all the answers – maofu Sep 23 '22 at 02:14