Git如何复用完整提交信息(主题+描述)?现有命令仅获主题求解决
Ah, I’ve run into this exact frustration before! When you use git commit --reuse-message or --reedit-message, it’s supposed to pull in the full commit message, but sometimes Git’s default behavior or edge cases can make it only grab the subject line. Let’s go through the reliable fixes that will get you the complete message every time:
Method 1: Directly Import the Full Message with git show
This is the most foolproof way to pull in the entire commit message (subject + body):
Run this command to reuse the message without editing:
git commit -F <(git show --format=%B YOUR_COMMIT_ID_OR_REF)The
--format=%Bflag tells Git to output the full, unmodified commit message (including all line breaks and body text). The<(...)syntax is a process substitution that passes the output directly togit commit -F(which reads the message from a "file").If you want to edit the message before committing, add the
--editflag:git commit --edit -F <(git show --format=%B YOUR_COMMIT_ID_OR_REF)This will open your default editor with the full commit message ready for tweaks.
For Windows Users (Command Prompt/PowerShell)
Process substitution doesn’t work natively in Windows shells, so use a temporary file instead:
- Export the full commit message to a file:
git show --format=%B YOUR_COMMIT_ID_OR_REF > commit-message.txt - Use the file to commit (with or without editing):
# No edit git commit -F commit-message.txt # With edit git commit --edit -F commit-message.txt - Clean up the temporary file when done:
del commit-message.txt
Why Your Original Commands Might Have Failed
In most cases, --reuse-message should work for full messages, but here are a few reasons it might not:
- Commit message formatting: If your original commit message uses unusual line breaks or comment characters (starting with
#), Git might truncate parts of it when using the reuse flags. - Git version quirks: Older Git versions (pre-2.20) had occasional bugs with message reuse for certain commit types (like merge commits). Updating Git can fix this, but the
git showmethod works across all versions. - Refspec issues: If you’re using a ref like
HEAD~1but the commit is a merge, Git might pull the merge message instead of the full feature commit message. Using the explicit commit ID avoids this ambiguity.
Bonus: Create an Alias for Ease
If you do this often, add an alias to your Git config to save typing:
git config --global alias.reuse-commit '!f() { git commit -F <(git show --format=%B "$1"); }; f'
Now you can just run:
git reuse-commit YOUR_COMMIT_ID_OR_REF
内容的提问来源于stack exchange,提问作者Marek Bocian

