Home > Software design >  git commit cannot convert \n to new line
git commit cannot convert \n to new line

Time:09-23

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

CodePudding user response:

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"

...

CodePudding user response:

This is working for me:

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

In bash, at least.

  • Related