核弹级的git指令 git filter-branch

这是一个可以修改提交历史的命令,威力很猛的后悔药。
 
 
1、彻底删除不需要的文件

 比如一不小心把 node_modules 目录加入git仓库了,导致仓库很大 :

git filter-branch -f --prune-empty --index-filter "git rm --cached --ignore-unmatch -fr ./node_modules" -- --all 

 

2、修改提交用户名

git filter-branch -f --env-filter "GIT_AUTHOR_NAME=fwindpeak" -- --all

  

3、修改提交邮箱

git filter-branch -f --env-filter "GIT_AUTHOR_EMAIL=fwindpeak@xxx.com" -- --all

 

注意,如果在windows下操作,必须用双引号。

 

其他更详细的操作,参考这里: https://git-scm.com/docs/git-filter-branch

 ----
 

新版 `git filter-repo`效果更好一点

 

## 修改用户名邮箱

 

git filter-repo --commit-callback '
if commit.author_name == b"oldName":
    commit.author_name = b"newName"
    commit.author_email = b"newName@gmail.com"
'

  

posted @ 2017-07-06 17:55  fwindpeak  阅读(5179)  评论(0编辑  收藏  举报