You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否基于分段提交信息自动组合生成完整的Git提交信息?

Yes, This Is Totally Achievable!

This workflow you're describing is absolutely doable—you just need a bit of custom tooling to tie together per-file messages into a single commit message. Let me walk you through two practical, easy-to-implement approaches.

Approach 1: Custom Shell Scripts (Most Straightforward)

This method mimics the exact workflow you outlined: you'll use a custom command to stage files with their associated messages, then another command to compile those messages into a commit.

Step 1: Create the "add with message" script

Make a script called git-add-with-msg (save it somewhere in your $PATH, like ~/bin/, or your project root) with this content:

#!/bin/bash
# Check if we have both a file and message
if [ $# -lt 2 ]; then
    echo "Usage: git-add-with-msg <file-path> \"Your message here\""
    exit 1
fi

FILE="$1"
MSG="$2"

# Stage the file like normal git add
git add "$FILE"

# Append the message to a temporary file in the .git directory
# We use .git so it's ignored by Git by default
echo "$MSG" >> .git/tmp_commit_messages

Give it executable permissions:

chmod +x git-add-with-msg

Step 2: Create the "commit from messages" script

Next, make a git-commit-from-msgs script:

#!/bin/bash
TMP_FILE=".git/tmp_commit_messages"

# Check if we have any saved messages
if [ ! -f "$TMP_FILE" ]; then
    echo "No staged messages found! Use git-add-with-msg to stage files with descriptions first."
    exit 1
fi

# Commit using the collected messages as the commit message
git commit -F "$TMP_FILE"

# Clean up the temporary file
rm "$TMP_FILE"

Again, make it executable:

chmod +x git-commit-from-msgs

How to use it

For your example files:

git-add-with-msg file1 "Changed file1..."
git-add-with-msg file2 "Changed file2..."
git-add-with-msg file3 "Changed file3..."

# Then generate the commit
git-commit-from-msgs

This will create a commit with the message exactly as you wanted:

Changed file1...
Changed file2...
Changed file3...

Approach 2: Use Git Notes + Commit Script

If you prefer to use built-in Git features instead of custom scripts, you can leverage git notes to attach messages to individual files, then compile those notes into a commit message.

How to use it

  1. Stage your files normally:
git add file1 file2 file3
  1. Attach a note to each staged file:
git notes add file1 -m "Changed file1..."
git notes add file2 -m "Changed file2..."
git notes add file3 -m "Changed file3..."
  1. Use this script to compile notes into a commit (save as git-commit-from-notes):
#!/bin/bash
COMMIT_MSG=""

# Loop through all staged files
for FILE in $(git diff --name-only --cached); do
    # Get the note for the file (suppress errors if no note exists)
    NOTE=$(git notes show "$FILE" 2>/dev/null)
    if [ -n "$NOTE" ]; then
        COMMIT_MSG+="$NOTE"$'\n'
        # Remove the note after we've used it
        git notes remove "$FILE"
    fi
done

if [ -z "$COMMIT_MSG" ]; then
    echo "No notes found for staged files."
    exit 1
fi

# Trim the trailing newline and commit
git commit -m "$(echo "$COMMIT_MSG" | sed '$ d')"

Make it executable, then run:

git-commit-from-notes

This will do the same thing as the first approach, using Git's native note system instead of a temporary file.

Final Notes

Both approaches work well—pick the one that fits your workflow best. The first method is more intuitive if you want to tie the message directly to the staging step, while the second uses Git's built-in tools which might feel cleaner to some users.

内容的提问来源于stack exchange,提问作者AkuLink1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 20:17:31