[git] Add line break to 'git commit -m' from the command line

I am using Git from the command line and am trying to add a line break to the commit message (using git commit -m "") without going into Vim.

Is this possible?

This question is related to git bash shell

The answer is


Certainly, how it's done depends on your shell. In Bash, you can use single quotes around the message and can 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" (also known as heredoc):

git commit -F- <<EOF
Message

goes
here
EOF

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."

Note that this creates separate paragraphs - not lines. So there will be a blank line between each two -m lines, e.g.:

My head line

My content line.

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.


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:

Multi-line Git commit message in PowerShell

The screenshot 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 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 ...

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.

So to edit your message using nano, for example, you can run:

export GIT_EDITOR=nano
git commit

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.


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.


I don't see anyone mentioning that if you don't provide a message it will open nano for you (at least in Linux) where you can write multiple lines...

Only this is needed:

git commit

Sadly, git doesn't seem to allow for any newline character in its message. There are various reasonable solutions already above, but when scripting, those are annoying. Here documents also work, but may also a bit too annoying to deal with (think yaml files)

Here is what I did:

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

While this is also still ugly, it allows for 'one-liners' which may be useful still. As usually the strings are variables or combined with variables, the uglynes may be kept to a minimum.


IMO the initial commit message line is supposed to be to short, to the point instead of paragraph. So using git commit -m "<short_message>" will suffice

After that in order to expand upon the initial commit message we can use

git commit --amend

which will open the vim and then we can enter the explanation for the commit message which in my opinion easier than command line.


Questions with git tag:

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to know the git username and email saved during configuration? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 fatal: ambiguous argument 'origin': unknown revision or path not in the working tree HTTP Basic: Access denied fatal: Authentication failed npm notice created a lockfile as package-lock.json. You should commit this file Do I commit the package-lock.json file created by npm 5? Abort a Git Merge key_load_public: invalid format git - remote add origin vs remote set-url origin Visual Studio 2017 - Git failed with a fatal error Get git branch name in Jenkins Pipeline/Jenkinsfile Changing the git user inside Visual Studio Code How to compare different branches in Visual Studio Code Git checkout - switching back to HEAD Clear git local cache Deleting a local branch with Git Rebuild Docker container on file changes Cloning specific branch How to add chmod permissions to file in Git? Git copy changes from one branch to another Git merge with force overwrite Project vs Repository in GitHub How to add a file to the last commit in git? Getting permission denied (public key) on gitlab Delete commit on gitlab gpg failed to sign the data fatal: failed to write commit object [Git 2.10.0] Remove a modified file from pull request Updates were rejected because the tip of your current branch is behind its remote counterpart Can't push to the heroku How to discard local changes and pull latest from GitHub repository In Visual Studio Code How do I merge between two local branches? error: RPC failed; curl transfer closed with outstanding read data remaining Change drive in git bash for windows Checkout Jenkins Pipeline Git SCM with credentials? How to fix git error: RPC failed; curl 56 GnuTLS Trying to pull files from my Github repository: "refusing to merge unrelated histories" Visual Studio Code how to resolve merge conflicts with git? merge one local branch into another local branch Can't push to remote branch, cannot be resolved to branch

Questions with bash tag:

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal? Copy Files from Windows to the Ubuntu Subsystem Docker: How to use bash with an Alpine based docker image? How to print / echo environment variables? Passing bash variable to jq gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now How to check if an environment variable exists and get its value? docker entrypoint running bash script gets "permission denied" Copy Paste in Bash on Ubuntu on Windows How to set env variable in Jupyter notebook How do I disable Git Credential Manager for Windows? How to set aliases in the Git Bash for Windows? MINGW64 "make build" error: "bash: make: command not found" Disable beep of Linux Bash on Windows 10 What does `set -x` do? psql: command not found Mac How do I use a regex in a shell script? nodemon not working: -bash: nodemon: command not found How to open google chrome from terminal? Get Environment Variable from Docker Container How to find files modified in last x minutes (find -mmin does not work as expected) How to pass arguments to Shell Script through docker run How to run C program on Mac OS X using Terminal? Curl command without using cache Running a script inside a docker container using shell script Creating an array from a text file in Bash Bash checking if string does not contain other string How to check if a Docker image with a specific tag exist locally? How do I edit $PATH (.bash_profile) on OSX? Raise error in a Bash script how to wait for first command to finish? I just assigned a variable, but echo $variable shows something else What do $? $0 $1 $2 mean in shell script? How to sort a file in-place How to read a .properties file which contains keys that have a period character using Shell script How can I remount my Android/system as read-write in a bash script using adb? How to get ip address of a server on Centos 7 in bash Changing an AIX password via script? How to remove last n characters from a string in Bash? Difference between ${} and $() in Bash List file using ls command in Linux with full path

Questions with shell tag:

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied" Interactive shell using Docker Compose How do I use a regex in a shell script? How to pass arguments to Shell Script through docker run Can pm2 run an 'npm start' script Running a script inside a docker container using shell script shell script to remove a file if it already exist Run a command shell in jenkins Raise error in a Bash script I just assigned a variable, but echo $variable shows something else What do $? $0 $1 $2 mean in shell script? How to sort a file in-place How to get a shell environment variable in a makefile? How to read a .properties file which contains keys that have a period character using Shell script Run multiple python scripts concurrently how to check which version of nltk, scikit learn installed? Changing an AIX password via script? Loop through a comma-separated shell variable How to use execvp() List file using ls command in Linux with full path How to use su command over adb shell? Display curl output in readable JSON format in Unix shell script How to check the exit status using an if statement How to solve ADB device unauthorized in Android ADB host device? Redirect echo output in shell script to logfile adb shell su works but adb root does not Run Python script at startup in Ubuntu Copy multiple files from one directory to another from Linux shell Relative paths based on file location instead of current working directory How to Batch Rename Files in a macOS Terminal? Speed up rsync with Simultaneous/Concurrent File Transfers? Multi-line string with extra space (preserved indentation) How to set child process' environment variable in Makefile Get MAC address using shell script How to force 'cp' to overwrite directory instead of creating another one inside? Multiple conditions in if statement shell script How do I change file permissions in Ubuntu How to get key names from JSON using jq Bash: Echoing a echo command with a variable in bash How/When does Execute Shell mark a build as failure in Jenkins? Shell Script: How to write a string to file and to stdout on console?