33

I want to improve the way I do git commits and I've been reading around the web. I followed the site here http://chris.beams.io/posts/git-commit/ and this lead me to https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration where I can set my default editor, but I still don't understand how I edit the subject line separately from the body of the commit.

I'm used to doing:

git commit -am "message here"

but as I understand it for longer commits, I should use an editor like vim (on my mac)

3
  • 6
    This separation into "subject line" and "body" is done by the client that displays the commit message. The first line of the commit message will become the "subject" usually.
    – Thilo
    Nov 9, 2016 at 11:17
  • 1
    How do I differentiate between the first and second line? How do I commit using the editor? Nov 9, 2016 at 11:17
  • 2
    With a line-break. Easier to do in a text editor.
    – Thilo
    Nov 9, 2016 at 11:18

2 Answers 2

60

The easiest way to do it is to run git commit without -m and the message.

(git commit -a is a shortcut for git add -u; git commit, with some other minor technical differences)

It will start an editor where you can enter the commit message on multiple lines. Then you save the file and when the editor exits, git commit continues and uses the message from the file.

If you prefer to do everything from a single command (or you write a script that needs to call git commit and this interactive way of committing is not an option) then you can provide the commit subject and the commit message body by using the -m argument two times:

git commit -m "this is the subject" -m "this is the body"

Using -m multiple times in the command line concatenates the messages as separate paragraphs (separated by an empty line). This works perfectly to provide the subject as the argument of the first -m and the message body as the argument of the second -m.

There is no easy way to embed newlines in the commit message body. Using three or more times the -m option will produce a commit message that contains empty lines and this is probably not what you want.

If you are on Linux or macOS and you shell of choice is bash then there is an ugly but working way to write a message body that contains new lines. Embed each line in quotes (") to allow them contain spaces and join the lines with $'\n' which is the bash way of writing special characters in the command line (or in a script).

The command looks like this:

git commit -m "the subject" -m "the first line"$'\n'"the second line"$'\n'"the third line"
5
  • 12
    In sh/bash, there's a maybe-less-ugly (but a bit error prone) way to embed newlines: just deliberately fail to close the quote, as in git commit -m subject -m "first line. When you enter this command, the shell switches from prompt #1 (the one usually ending with $) to prompt #2 (usually ending with >) and you can continue typing. A line with just one double quote terminates the input, so you might enter: the second line and then the third line". I'm not particularly recommending this, just offering it as an alternative to the $'\n' syntax.
    – torek
    Nov 9, 2016 at 16:55
  • 1
    @torek - This is great alternative. Very clean and practical. Thank you!
    – xZero
    Aug 15, 2017 at 9:33
  • @torek Great easy clean solution.
    – Amr
    Sep 26, 2018 at 22:22
  • just beeing curious: can u concatenate \0x10 [ascii newline] ?
    – clockw0rk
    Aug 7, 2020 at 9:41
  • @clockw0rk \x10 is not a new line. The character you are looking for (line feed) is \x0a; which is the same as \n.
    – axiac
    Aug 8, 2020 at 12:58
8

Run: git commit -a and you should get a VI editor interface that lets you enter Subject line and message. After finishing entering text press ESC then :wq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.