# 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**: 2025-05-27 ## 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 ``` 本地环境变量如果是python,下面脚本正常发使用,如果是python3,需要修改脚本 python为python3 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 modifyTimesByHash.sh 268f9926c94f563be11660ec928e085d0bede101 "2023-13-32 21:15:55" ``` ###### 3.2 修改多个提交时间 **获取最早的提交(即第一次提交)**: 使用 `--reverse` 参数可以让 `git log` 按照时间升序输出提交记录,然后我们只需要取第一个提交的时间。 ```bash git log --reverse --format=%cd --date=iso | head -n 1 ``` `output : 2023-09-01 09:27:27 +0800` **获取最晚的提交(即最近一次提交)**: 默认情况下,`git log` 是按照时间降序输出的,所以我们直接取第一个提交的时间即可。 ```bash git log --format=%cd --date=iso | head -n 1 ``` `output : 2023-12-04 17:41:44 +0800` 显示所有分支的提交次数 ```sh git rev-list --all | sort -u | wc -l ``` `output : 392` 首先extractionTime.sh脚本会生成一个txt `commit_hashes_and_times.txt`, 里面包含所有提交的hash、提交时间、时间戳 `debcf066c7b1ca723da030645b23ffa498ba52a4,1733200677,2023-12-01 12:31:34` 然后generateTime.sh脚本会生成新的时间。 在generateTime.sh中,设置开始时间和结束时间,会重新生成新的时间, 注意,这个时间是根据提交次数来计算的,所以,如果提交次数很少,那么时间间隔会很大。 关于生成新日期的算法,根据目标日期和提交次数,计算出一个时间间隔,然后根据这个时间间隔,生成新的日期,如果不能整除, 其中,设x为总提交数除以总天数,y为总提交数除以总天数的余数,total为总提交数,day为总天数。 ````bash total 总提交数 假设给定为2000条 day 1.1--9.30计算出来的工作日天数 day计算的时候,记得减掉法定节假日和周六日的天数 以下都为正整数 x = total / day y = total % day total = (x + 1) * y + x * (day - y) 先通过总提交数和工作日天数,计算出来的 x 和 y 然后前y天生成(x+1)个9到18点的时间(时间是随机的,并且是递增的), (day-y)天,生成x个9到18点的时间(时间是随机的,并且是递增的)确保一共生成了total条. ```` `x = total / day` `y = total % day` `total = (x+1)*y + x*(day-y)` `holiday.sh`中为近几年的法定节假日和周末,生成日期时候将排除这些时间。 ` start_date="2023-07-01" end_date="2023-12-04" ` commit_hashes_and_times.txt内容如下: `debcf066c7b1ca723da030645b23ffa498ba52a4,1733200677,2023-12-04 11:45:25` 最后modifyTimes.sh脚本会根据这两个文件,批量修改提交时间。 ```bash # 收集项目所有的时间 sh extractionTime.sh "2023-09-01 09:27:27" "2023-12-04 17:41:44" # 生成想要的时间,记得修改sh脚本中的开始时间和结束时间 sh generateTime.sh # 批量编辑时间 sh modifyTimes.sh ./xx.txt ``` ##### 4.修改内容 修改项目的内容,匹配项目中所有的内容,需要注意全局替换,确保你要替换的内容是唯一的,才不会影响其他位置的代码。 ```bash sh modifyContent.sh "test" "test1" ```