Readme.md
· 1.6 KiB · Markdown
Sin formato
首先修改`callback.py`的邮箱和名称信息,然后将它放到需要修改Git记录的项目文件夹根目录下
执行:
```sh
git-filter-repo --commit-callback callback.py
```
可能会提示报错:
```
Aborting: Refusing to destructively overwrite repo history since
this does not look like a fresh clone.
(expected freshly packed repo)
Please operate on a fresh clone instead. If you want to proceed
anyway, use --force.
```
**为什么会出现这个错误**?
git-filter-repo 会检查当前仓库的状态,如果它检测到:
- 存在远程跟踪分支(比如 origin/main);
- 有未推送的提交;
- 或者仓库不是刚克隆下来且打包过的状态;
它就会认为这是一个“非安全”环境,因此拒绝执行,防止你意外丢失数据。
如果你确定你的本地提交都已经推送到远端,确定不会丢失任何改动后,可以在原命令的基础上加上 `--force`:
```sh
git-filter-repo --commit-callback callback.py --force
```
成功执行的话,可能会看到如下信息:
```
Parsed 21 commits
New history written in 1.59 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at 9161924 更新API链接域名
Enumerating objects: 1190, done.
Counting objects: 100% (1190/1190), done.
Delta compression using up to 12 threads
Compressing objects: 100% (849/849), done.
Writing objects: 100% (1190/1190), done.
Total 1190 (delta 181), reused 1152 (delta 173), pack-reused 0 (from 0)
Completely finished after 2.47 seconds.
```
接下来再强制推送到远端就可以更新原来的Git提交历史了
首先修改callback.py的邮箱和名称信息,然后将它放到需要修改Git记录的项目文件夹根目录下
执行:
git-filter-repo --commit-callback callback.py
可能会提示报错:
Aborting: Refusing to destructively overwrite repo history since
this does not look like a fresh clone.
(expected freshly packed repo)
Please operate on a fresh clone instead. If you want to proceed
anyway, use --force.
为什么会出现这个错误?
git-filter-repo 会检查当前仓库的状态,如果它检测到:
- 存在远程跟踪分支(比如 origin/main);
- 有未推送的提交;
- 或者仓库不是刚克隆下来且打包过的状态;
它就会认为这是一个“非安全”环境,因此拒绝执行,防止你意外丢失数据。
如果你确定你的本地提交都已经推送到远端,确定不会丢失任何改动后,可以在原命令的基础上加上 --force:
git-filter-repo --commit-callback callback.py --force
成功执行的话,可能会看到如下信息:
Parsed 21 commits
New history written in 1.59 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at 9161924 更新API链接域名
Enumerating objects: 1190, done.
Counting objects: 100% (1190/1190), done.
Delta compression using up to 12 threads
Compressing objects: 100% (849/849), done.
Writing objects: 100% (1190/1190), done.
Total 1190 (delta 181), reused 1152 (delta 173), pack-reused 0 (from 0)
Completely finished after 2.47 seconds.
接下来再强制推送到远端就可以更新原来的Git提交历史了
callback.py
· 333 B · Python
Sin formato
old_email = b"[email protected]"
new_email = b"[email protected]"
new_name = "newname"
if commit.author_email == old_email:
commit.author_name = new_name.encode("utf-8")
commit.author_email = new_email
if commit.committer_email == old_email:
commit.committer_name = new_name.encode("utf-8")
commit.committer_email = new_email
| 1 | old_email = b"[email protected]" |
| 2 | new_email = b"[email protected]" |
| 3 | new_name = "newname" |
| 4 | |
| 5 | if commit.author_email == old_email: |
| 6 | commit.author_name = new_name.encode("utf-8") |
| 7 | commit.author_email = new_email |
| 8 | if commit.committer_email == old_email: |
| 9 | commit.committer_name = new_name.encode("utf-8") |
| 10 | commit.committer_email = new_email |
| 11 |