Git 存储在变量中时提交消息混乱

Git commit message messed up when stored in a variable

我有一个 Git 提交,其中有一个摘要,然后是一些描述。所以当我通过 git log --format=%B -n 1 <commit> 看到提交消息时,它看起来像这样:

Commit Summary Line * Commit Description Line 1 * Commit Description Line 2 * Commit Description Line 3

但是,当我尝试将其存储在 Bash 变量中时,因此:

message=$(git log --format=%B -n 1 <commit>)

然后我尝试 echo $message,我从当前目录中获取文件夹名称,并将其与提交消息中的每一行混合在一起。更重要的是,我什至没有看到提交消息中的所有行,只是其中的一些。所以,$message 看起来像这样:

Commit Summary Line folder1 folder2 folder3 Commit Description Line 1 folder1 folder2 folder3 Commit Description Line 3

对这种行为有什么解释吗?我只希望 $message 包含完整提交消息中的所有行。我什至不在乎它们是在新行中还是全部在一行中,我只希望所有行都存储在一个字符串变量中。我该如何实现?

好像是有点流氓pathname expansion在这里玩造成的*。尝试在您的 message 变量周围添加一对引号,您应该会成功!

echo "$message"