问题
When I use git log
to check out my commit explanatory note I want it to look like this:
1. what I changed
2. blank line
3. why I changed it
...being in 3 lines not 1 like this:
1. what i changed 2. blank line 3. why i changed
However, git log
shows it in one line. So, how do I get this to happen using git commit -m
?
回答1:
You just use the following command:
$ git commit -m "1. what i changed
> 2. blank line
> 3. why i changed"
In your terminal, just hit 'enter' for a new line. The commit message won't end until you add the closing quote. The git log will look like:
$ git log
commit abcde2f660c707br2d20411581c4183170c3p0c2
Author: Alex Pan <alexpan@stackoverflow.com>
Date: Tue Apr 28 20:52:44 2015 -0700
1. what i changed
2. blank line
3. why i changed
回答2:
Excerpt from documentation
-m <msg> --message=<msg>
Use the given as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.
In your case it does exactly what you want, inserts a blank line between first and second lines
git commit -m "what I changed" -m "why I changed it"
This is useful if you want to amend previously added comment
回答3:
The multiple-line format you describe is the recommended one with Git (See DISCUSSION in the documentation of git commit). The simplest way to do it is to use git commit
without -m
, and write your message in your text editor.
回答4:
I find it much easier to save the commit message to a file, and then use the -F option.
Example:
$ cat > /tmp/commit_msg.txt
DE123 bug fix: incorrect fetching of instance details
- fixed this and that
- also did such and such
$ git commit -F /tmp/commit_msg.txt
You could also use an editor to edit the message file before the commit.
回答5:
Rather than use a temp file when trying to do this programmatically you can use stdin
git commit -F-
then write the message to stdin
回答6:
I needed to have a bash script do multi-line git commits for me, so here's two options I came up with:
Write to a temporary file then commit with the contents of the file as the message:
printf "first line\nsecond line\nthird line" > "file.txt" git commit -F "file.txt"
(My preferred approach): Write to a temporary variable then commit with the contents of the variable as the message. Note that the quotes around
$MSG
when doing any command to recall the contents of the variable are required! Without them, you'll lose your newlines.MSG="$(printf "first line\nsecond line\nthird line")" git commit -m "$MSG"
As an extension of this 2nd approach, in case you need to script building the message in multiple pieces or steps, that is possible too. WATCH OUT though! Notice where I place my newline (
\n
) characters. I do NOT place them at the end of anyprintf
string. That's because if I do they will get gobbled up, because bash automatically removes any trailing newline characters, since it's dumb like that. So, do it like this instead, which works just fine:MSG="$(printf "first line")" MSG="$(printf "${MSG}\nsecond line")" MSG="$(printf "${MSG}\nthird line")" git commit -m "$MSG"
Sample git log
output from any of the above git commit
s:
commit e1983659c6ae2e9d2eb4332657329837582fc32b (HEAD -> master)
Author: Gabriel Staples <email@gmail.com>
Date: Tue Mar 24 00:55:31 2020 -0700
first line
second line
third line
References:
- Unix & Linux: "Why does shell Command Substitution gobble up a trailing newline char?"
- VERY USEFUL! ==> How can I have a newline in a string in sh? <==
来源:https://stackoverflow.com/questions/29933349/how-can-i-make-git-commit-messages-divide-into-multiple-lines