Git提交摘要与详情存储位置及.git目录完整提交消息文件查询
Great question—let’s break this down so you understand exactly where Git stores your commit data, and how to get those full commit messages into a text file like you want.
Core Storage: Git’s Object Database
Git saves every commit (including its full message—summary and details) as a compressed object in the .git/objects/ directory. Each commit gets a unique SHA-1 hash: the first two characters of the hash become a subdirectory name, and the remaining characters are the filename of the compressed object. For example:
- If your commit hash is
7a3f9b2d1e, the object lives at.git/objects/7a/3f9b2d1e...
Heads Up: These Files Are Compressed
Don’t try opening these files directly in a text editor—they’re zlib-compressed, so you’ll only see gibberish. Git provides built-in tools to decompress and view their content.
How to Export Full Commit Messages to a Text File
Git doesn’t store uncompressed plain-text files of old commit messages by default (it’s all about storage efficiency), but you can easily export them manually:
- Grab the commit hash you want to view. Run
git logto see your commit history—each entry starts with a hash (you only need the first 5-7 characters, not the full string). - Export the full message to a text file using one of these commands:
- To get the full commit data (message + author/date metadata) in a readable format:
git cat-file -p <commit-hash> > my-commit.txt - If you only want the raw commit message (no metadata), use this:
Thegit log --format=%B -n 1 <commit-hash> > my-commit-message.txt%Bflag pulls the full raw message, and-n 1limits output to just that one commit.
- To get the full commit data (message + author/date metadata) in a readable format:
Clarifying the Files You Mentioned
To confirm your observations:
.git/COMMIT_EDITMSG: This is just a temporary file that holds the most recent commit message you edited (either for an in-progress commit or the last one you made). It never stores historical commit data.- Default
git logoutput: It only shows the commit summary by design. To view the full message in the console, usegit log --pretty=fullorgit show <commit-hash>(which also displays the commit’s code changes alongside the message).
Git’s design prioritizes efficiency over easy plain-text access to old commits, but the export commands above let you get the full message into a text file whenever you need it.
内容的提问来源于stack exchange,提问作者chharvey

