# git-modification **Repository Path**: chun_hui_du/git-modification ## Basic Information - **Project Name**: git-modification - **Description**: 用来修改git相关信息:提交作者、提交邮箱、提交commit message,提交时间 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://gitee.com/chun_hui_du/git-modification - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2025-01-22 - **Last Updated**: 2026-07-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # git修改 #### 介绍 基于git-filter-repo(https://github.com/newren/git-filter-repo) 用来修改git相关信息。提交人、提交邮箱、提交commit message,提交时间、提交内容。 #### 使用说明 git版本 >= 2.22 ,python >= 3.6 ```bash git --version python --version or python3 --version pip install pytz ``` 本地环境变量如果是python,下面脚本正常发使用,如果是python3,需要修改脚本 python为python3。另外批量修改时间功能依赖 Python 包 `pytz`,需通过 `pip install pytz` 安装。 window和linux环境都可以使用,window建议使用git bash 使用之前,注意备份好代码 ##### 1.修改提交人和邮箱 获取提交次数 提交人姓名 提交邮箱 ```bash git log --all --format='%aN <%cE> ' | sort | uniq -c | sort -nr ``` 根据提交次数排序,选择一个提交人,作为新的提交人,修改提交人和邮箱 ```bash sh modifyAhthor.sh Mark Li Li@xxx.com ``` 根据Mark为索引,将Mark的提交人修改为Li,提交邮箱修改为Li@xxx.com,注意,这个修改是针对全部commit的修改。 ##### 2.修改message ```bash sh modifyMessage.sh a b ``` 注意,这个修改是针对全部commit的修改,确保你要修改的内容a是唯一的,才不会影响其他位置的commit。 ##### 3.修改提交时间 ###### 3.1 修改单个提交时间 ```bash sh modifyTimeByHash.sh 268f9926c94f563be11660ec928e085d0bede101 "2023-12-13 21:15:55" ``` ###### 3.2 批量修改提交时间 分 3 步,在目标 git 仓库根目录下执行(脚本路径根据实际位置调整)。 **第 1 步:提取原始时间** ```bash sh ../git-modification/extractionTime.sh "2024-01-01 09:00:00" "2024-09-30 18:00:00" ``` 两个参数是筛选范围——只有落在这个时间范围内的 commit 才会被提取。运行后生成 `commit_hashes_and_times.txt`,内容格式为 `哈希,时间戳,YYYY-MM-DD HH:MM:SS`。 **第 2 步:生成新时间** ```bash sh ../git-modification/generateTime.sh "2024-01-01" "2024-09-30" ``` 脚本会读取 `commit_hashes_and_times.txt`,在指定日期范围内的工作日(排除 `holiday.sh` 中定义的法定节假日和周末)随机分配 09:00-18:00 的时间。 分配算法:设工作日天数为 day,总提交数为 total,则每条 commit 按 `x = total / day`、`y = total % day` 分配——前 y 个工作日每天分配 x+1 条,其余每天分配 x 条,确保每天的时间递增且总量等于 total。 **第 3 步:应用新时间** 提供两种方式: ```bash # 逐条模式 —— 每条 commit 调用一次 git-filter-repo,可观察逐条进度 sh ../git-modification/modifyTimes.sh commit_hashes_and_times.txt # 批量模式(推荐)—— 单次调用 git-filter-repo,速度快 sh ../git-modification/modifyTimesOnce.sh commit_hashes_and_times.txt ``` 两者按原始时间戳匹配 commit,替换 author_date 和 committer_date,结果一致。 ##### 4.修改内容 修改项目的内容,匹配项目中所有的内容,需要注意全局替换,确保你要替换的内容是唯一的,才不会影响其他位置的代码。 ```bash sh modifyContent.sh "test" "test1" ``` ##### 5.删除提交 从历史中完全删除指定的提交,支持一次删除多个。被删除提交的 commit 记录和文件修改会一并清除,子提交自动 rebase 到父提交上。 ```bash # 安全模式(需要 fresh clone) sh deleteCommit.sh 268f9926c94f563be11660ec928e085d0bede101 sh deleteCommit.sh hash1 hash2 hash3 # 强制模式(跳过 fresh clone 检查) sh deleteCommit.sh --force hash1 hash2 ``` > 删除后仓库历史相当于该提交从未存在过。如果子提交与被删提交修改了相同文件的相同位置,rebase 时可能产生冲突,需手动处理。 ##### 6.通过 Excel 批量修改提交(git_history_xlsx.py) Python 脚本,适合一次性修改多条提交的作者、邮箱、提交信息和时间。依赖 `openpyxl`: ```bash pip install openpyxl ``` **导出 → 编辑 → 导入 三步流程:** ```bash # 1. 导出当前分支所有提交到 xlsx python ../git-modification/git_history_xlsx.py --repo . export commits.xlsx # 2. 用 Excel 编辑 commits.xlsx(改 author_name、author_email、message、commit_time) # 不要动 hash 列,删除不需要修改的行 # 3. 导入修改 python ../git-modification/git_history_xlsx.py --repo . apply commits.xlsx --force # 4. 推送 git push --force-with-lease origin HEAD:<分支名> ``` > xlsx 中的 commit_time 格式为 `YYYY-MM-DD HH:MM:SS +0800`,时区固定 +0800。`--force` 在非 fresh clone 仓库上必须使用。