Git 重写历史:永久删除文件、修改历史提交信息

永久删除 Git 提交的文件,减小 .git 文件夹大小。

这里使用 BFG:https://rtyley.github.io/bfg-repo-cleaner/

# 使用 --mirror(--bare) 参数 clone git repo
git clone --mirror git://example.com/some-big-repo.git

# 删除 100M 以上的文件 
java -jar bfg.jar --strip-blobs-bigger-than 100M some-big-repo.git
# 删除 test.sdf 文件 
java -jar bfg.jar --delete-files test.sdf some-big-repo.git
# 删除 _Boot 文件夹,且忽略 HEAD 版本保护
java -jar bfg.jar --delete-folders _Boot --no-blob-protection some-big-repo.git

# 推送到 Git
cd some-big-repo.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push

 

修改所有历史提交信息

其它属性:https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables

git filter-branch --commit-filter '
    if [ "$GIT_AUTHOR_NAME" = "jhxxb" ];
    then
        GIT_AUTHOR_NAME="jhxxb";
        GIT_AUTHOR_EMAIL="5114964+jhxxb@user.noreply.gitee.com";
        GIT_COMMITTER_NAME="jhxxb";
        GIT_COMMITTER_EMAIL="5114964+jhxxb@user.noreply.gitee.com";
        git commit-tree "$@";
    else
        git commit-tree "$@";
    fi' HEAD
git push -f

Git 推荐使用 https://github.com/newren/git-filter-repo,需要安装 python

 

修改指定历史提交信息

# 找到要修改记录的前一个记录 id
git log

# 修改,这里有几种修改选择:
# pick:保留该 commit
# reword:保留该 commit,但我需要修改该commit的 Message
# edit:保留该 commit, 但我要停下来修改该提交(包括修改文件)
# squash:将该 commit 和前一个 commit 合并
# fixup:将该 commit 和前一个 commit 合并,但我不要保留该提交的注释信息
# exec:执行 shell 命令
# drop:丢弃这个 commit
git rebase -i 7a8316f2865050501ea535b499f577ee989d65e1

# 将要修改记录开头的 pick 修改为 reword 保存,就会弹出修改界面,修改提交内容后再次保存,然后推送
git push -f

 


https://git-scm.com/book/zh/v2/Git-工具-重写历史

https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/removing-sensitive-data-from-a-repository

https://www.cnblogs.com/huipengly/p/8424096.html

https://www.cnblogs.com/flying_bat/p/4172435.html

posted @ 2020-10-10 15:27  江湖小小白  阅读(586)  评论(0编辑  收藏  举报