git删除commit历史记录

[TOC]

有时候你不小心将一些私人信息或者公司相关信息不小心上传到git上,虽然你重新提交,但是历史版本还是可以在git上看到,这时候就需要删除git上commit历史记录

方法一:

亲测

  • 思路是新建一个分支,把原来master分支上的东西拷过来,接着删除master分支,然后重命名成master,提交
  • 这个方法比较狠,master上所有的历史提交都会没了

如何删除这些历史记录,形成一个全新的仓库,并且保持代码不变呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1.Checkout
git checkout --orphan latest_branch
2. Add all the files
git add -A
3. Commit the changes
git commit -am "commit message"
4. Delete the branch
git branch -D master
5.Rename the current branch to master
git branch -m master
6.Finally, force update your repository
git push -f origin master

方法二

使用 BFG Repo-Cleaner 这个工具,几分钟内就删除干净了,.git 目录大小由原来的 2.9G 左右缩减到 70M,删除过去提交的所有 tar.gz,zip,jar,war 等,只留下源码,总算能 push 到 github 了。

具体执行流程为:

1
2
3
4
5
bfg --delete-files *.tar.gz # 后面的文件名匹配可换成自己要删除的文件名
git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push --all --force
git push --all --tags --force

热评文章