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

同一分支执行相同git log统计命令返回结果不一致问题

Troubleshooting Inconsistent Git Log Stats Results

Let's break down why your command might return different results on the same branch, and how to fix it:

First, here's your original command for reference:

git log --shortstat --author="cuongnq|hung.nguyen|thinhnv" --since="2018-03-26" --until="2018-04-12" --no-merges | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; ratio=deleted/inserted} END {printf "Commit stats:\n- Files changed (total).. %s\n- Lines added (total).... %s\n- Lines deleted (total).. %s\n- Total lines (delta).. %s\n- Deleted/Inserted ratio.. %.2f\n", files, inserted, deleted, delta, ratio}'

Common Causes for Inconsistent Results

1. The Branch Has Changed Between Runs

Even if you're targeting the same branch, inconsistencies can happen if:

  • Someone pushed new commits to the remote branch and you haven't pulled them
  • You or a teammate used git commit --amend, git rebase, or git push --force to rewrite commit history
  • You're in a detached HEAD state with uncommitted changes affecting the log

Fix:
Before running your stats command, lock your branch to a consistent state:

# Fetch latest remote changes and reset your local branch to match the remote
git fetch origin
git checkout your-branch-name
git reset --hard origin/your-branch-name

2. Time Filtering Uses Commit Time (Not Author Time)

By default, --since and --until filter commits based on their commit time—the timestamp when the commit was finalized, which can change if history is rewritten. If commits in your target range have modified commit times between runs, some may be included or excluded unexpectedly.

Fix:
Use --author-since and --author-until to filter based on the author's original commit time (this doesn't change when history is rewritten):

git log --shortstat --author="cuongnq|hung.nguyen|thinhnv" --author-since="2018-03-26" --author-until="2018-04-12" --no-merges

3. Author Regex Matching Issues

Older Git versions use basic regular expressions for the --author parameter, where | (OR) and . (wildcard) are treated as literal characters. Your current regex might not match all three authors correctly in older Git, leading to inconsistent commit selection.

Fix:
Escape special characters for basic regex compatibility:

--author="cuongnq\|hung\.nguyen\|thinhnv"

4. Fragile Pipe/Grep/Awk Logic

Your pipeline relies on parsing --shortstat output, which has edge cases (e.g., commits with 0 insertions or deletions) that break your awk logic. For example, a commit with 0 insertions(+) will cause a division-by-zero error for the ratio, leading to miscalculations.

Fix:
Switch to --numstat for more reliable parsing, and update the awk script to handle edge cases:

git log --numstat --author="cuongnq\|hung\.nguyen\|thinhnv" --author-since="2018-03-26" --author-until="2018-04-12" --no-merges | awk '
    NF == 3 {  # Only process lines with 3 fields (additions, deletions, filename)
        files++
        inserted += $1 + 0  # +0 ensures empty values are treated as 0
        deleted += $2 + 0
    }
    END {
        delta = inserted - deleted
        ratio = inserted > 0 ? deleted / inserted : 0  # Avoid division by zero
        printf "Commit stats:\n"
        printf "- Files changed (total).. %d\n", files
        printf "- Lines added (total).... %d\n", inserted
        printf "- Lines deleted (total).. %d\n", deleted
        printf "- Total lines (delta).. %d\n", delta
        printf "- Deleted/Inserted ratio.. %.2f\n", ratio
    }
'

--numstat outputs one line per changed file with explicit counts, making parsing far more consistent than --shortstat.

Final Notes

Start with the simplest checks first: confirm your branch is unchanged, then verify the author regex and time filtering. The --numstat version of the command is the most robust long-term solution for consistent stats.

内容的提问来源于stack exchange,提问作者Quan Nguyen Duc

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:13:22