Add line break to git commit -m from command line
I am using git from the command line, and trying to add a line break to the commit message (using git commit -m “”) without going into
Is this possible?
12 Solutions collect form web for “Add line break to git commit -m from command line”
Certainly, how it’s done depends on your shell. In bash, you can use single quotes around the message, and just leave the quote open, which will make bash prompt for another line, until you close the quote. Like this:
git commit -m 'Message goes here'
Alternatively, you can use a “here document”:
git commit -F- <<EOF Message goes here EOF
Using git from the command line with bash you can do the following:
git commit -m "this is > a line > with new lines > maybe"
Simply type and press Enter when you want a new line, the “>” symbol means that you have pressed Enter and there is a new line. Other answers work also.
If you just want, say, a head line and a content line, you can use:
git commit -m "My head line" -m "My content line."
You should be able to use
git commit -m $'first line\nsecond line'
From the Bash manual:
Words of the form $’string‘ are treated specially. The word expands to
string, with backslash-escaped characters replaced as specified by the
ANSI C standard.
This includes support for newlines as shown above, plus hex and Unicode codes and others. Go to the linked section to see a list of the backslash-escaped characters.
Adding line breaks to your git commit
Try the following to create a multi-line commit message:
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."
Then verify what you’ve done:
git log -1
You should end up with something like this:
The screen shot is from an example I set up using Powershell with Poshgit.
Doing something like:
git commit -m"test\ntest"
doesn’t work, but something like:
git commit -m"$(echo -e "test\ntest")"
works, but its not very pretty. You setup 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"
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.
I hope this isn’t leading too far away from the posted question but wouldn’t setting the default editor and then using
git commit -e
be much more comfortable?
From git documentation
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"
You could use
git commit -m "$(echo)" or
git commit -m $'\n'
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
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
That’s what I have in my
export ALTERNATE_EDITOR='' export EDITOR='emacsclient -t' export VISUAL='emacsclient -c' export SUDO_EDITOR='emacsclient -t'