个人项目,不需要协作,所以才敢这么折腾。 今天翻 git log ,发现历史里一堆 "update"、"fix"、"改一下" 这种毫无信息量的 commit message ,看着就难受。于是让 AI 把所有这类"废话 message"挑出来,结合实际 diff 分析改动内容,重新生成了有意义的描述。担心它改错,顺手让它写了个批处理 bash 脚本:
bash#!/usr/bin/env bash
set -euo pipefail
# 用法:./rewrite.sh [分支名] [--yes] [--dry-run]
# 旧 commit hash -> 新 message
declare -A MESSAGES=(
["69298b6141"]="chore(tasks): remove vendored tasks source"
["b3aaeff723"]="chore(claude): add local permissions config"
["032c032074"]="fix(testing): set mobile initial path"
# ... 其他映射
)
# 解析参数
TARGET="${1:-$(git branch --show-current)}"
AUTO_YES=0
DRY_RUN=0
[[ "$*" =~ --yes ]] && AUTO_YES=1
[[ "$*" =~ --dry-run ]] && DRY_RUN=1
# 预览变更
echo "目标分支: $TARGET"
for commit in "${!MESSAGES[@]}"; do
echo " $commit -> ${MESSAGES[$commit]}"
done
# dry-run 模式
if [ $DRY_RUN -eq 1 ]; then
echo "干运行模式,不实际修改"
exit 0
fi
# 二次确认
if [ $AUTO_YES -eq 0 ]; then
read -p "确认重写提交消息?[y/N] " -r
[[ ! $REPLY =~ ^[Yy] ]] && exit 0
fi
# 自动备份
BACKUP="backup-$(date +%Y%m%d-%H%M%S)"
git branch "$BACKUP" "$TARGET"
# 执行重写
git filter-branch -f --msg-filter '
case "$GIT_COMMIT" in
'"$(for hash in "${!MESSAGES[@]}"; do
echo "$hash) echo \"${MESSAGES[$hash]}\" ;;"
done)"'
*) cat ;;
esac
' -- "$TARGET"
echo "完成!备份分支: $BACKUP"
echo "如需恢复: git reset --hard $BACKUP"跑完脚本,看着 git log 整整齐齐,莫名被治愈了。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.