Git filter-repo 使用示例

Git filter-repo 是一个强大的工具,可以用来修改 Git 仓库的历史记录。它允许你删除文件、敏感信息、合并提交、保留指定分支等操作,还可以用它来修改提交历史中的信息。

1. 删除文件夹

git filter-repo --path 'myfolder/' --invert-paths

2. 删除指定文件

git filter-repo --path 'myfile.txt' --invert-paths

3. 删除提交历史中的敏感信息

git filter-repo --replace-text '<(echo 'password==>********')'

4. 保留指定分支

git filter-repo --refs refs/heads/master

5. 删除指定作者的提交历史

git filter-repo --commit-callback ' 
 if commit.author.name == 'John Doe': 
  return None 
 else: 
  return commit 
'

6. 保留指定时间范围内的提交历史

git filter-repo --commit-callback ' 
 if commit.commit_time < 1609459200 or commit.commit_time > 1612137599: 
  return None 
 else: 
  return commit 
'

7. 合并多个提交历史

git filter-repo --commit-callback ' 
 if commit.message.startswith('Merge pull request'): 
  return None 
 else: 
  return commit 
'

8. 替换指定文件中的字符串

git filter-repo --replace-text 'myfile.txt' --replace 'password' '********'

注意:

  • 使用 git filter-repo 前,请务必备份你的仓库。
  • git filter-repo 会修改你的 Git 仓库的历史记录,请谨慎使用。
  • 详细的 git filter-repo 使用方法请参考官方文档: https://github.com/newren/git-filter-repo
Git filter-repo 使用指南:删除文件、敏感信息、合并提交等

原文地址: https://www.cveoy.top/t/topic/lPtY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录