git-filter-repo:快速重写 Git 存储库历史记录
git filter-repo 是一个重写历史的通用工具,其中包括我在其他地方找不到的功能。它大致属于与 git filter-branch 相同的工具空间,但没有导致性能下降的问题,具有更多的功能,并且设计可以在可用性方面扩展,超出琐碎的重写情况。git 项目现在推荐 git filter-repo 而不是 git filter-branch。
安装
# 直接下载二进制文件 https://github.com/newren/git-filter-repo/blob/main/git-filter-repo
curl -SL -O https://fastfile.asfd.cn/github.com/newren/git-filter-repo/raw/refs/heads/main/git-filter-repo
chmod +x git-filter-repo
sudo mv git-filter-repo /usr/local/bin/
# 使用 pip 等包管理器工具
pip install git-filter-repo
基本信息
git filter-repo --commit-callback '
from pprint import pprint
pprint(commit.__dict__)
print("----")
'
{'author_date': b'1498786371 +0800',
'author_email': b'hello@example.com',
'author_name': b'Hello Chan',
'branch': b'refs/heads/v1.1.x',
'committer_date': b'1740737855 +0800',
'committer_email': b'hello@example.com',
'committer_name': b'Hello Chan',
'dumped': 0,
'encoding': None,
'file_changes': [<__main__.FileChange object at 0x754d632b5550>],
'id': 148,
'message': b'\xe4\xbf\xae\xe6\x94\xb9README\n',
'old_id': 148,
'original_id': b'5be357f660f7f7c5f425b4aebd0f3b906103eabf',
'parents': [60],
'type': 'commit'}
基本使用教程
# 修改邮箱
git-filter-repo --email-callback 'return email.replace(b"example.cn", b"example.com")' --force
# 或
git filter-repo --commit-callback '
if commit.committer_email == b"hello@example.cn":
commit.committer_name = b"Hello Chan"
commit.committer_email = b"hello@example.com"
' --force
# 修改提交信息
git-filter-repo --message-callback 'return message.replace(b"hi", b"hello")' --force --refs main~1..main
# 或
git-filter-repo --message-callback '
message=b"bugfix: " + message
return message
' --refs main~1..main --force
# 根据文件中的规则
# replace.txt,左侧修改为右侧
echo 'hi==>hello' > replace.txt
git filter-repo --replace-text replace.txt
更多教程:https://html.skiy.net/?https://github.com/newren/git-filter-repo/blob/docs/html/git-filter-repo.html
https://github.com/newren/git-filter-repo