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)
On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter. In your text editor, edit the commit message, and save the commit. You can add a co-author by adding a trailer to the commit.
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 .; git commit
)
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With