git 清除所有历史记录

有些时候,git 仓库累积了太多无用的历史更改,导致 clone 文件过大。如果确定历史更改没有意义,可以采用下述方法清空历史,

  1. 先 clone 项目到本地目录 (以名为 cnblog-tool的仓库为例)
 $ git clone git@gitee.com:makalochen/cnblog-tool.git
  1. 进入 cnblog-tool仓库,拉一个分支,比如名为 latest_branch
$ git checkout --orphan latest_branch
  1. 添加所有文件到上述分支 (Optional)
$ git add -A
  1. 提交一次
$ git commit -am "Initial commit."
  1. 删除 master 分支
$ git branch -D master
  1. 更改当前分支为 master 分支
$ git branch -m master
  1. 将本地所有更改 push 到远程仓库
$ git push -f origin master
  1. 关联本地 master 到远程 master
$ git branch --set-upstream-to=origin/master

注意:对 gitee 用户,因为 gitee 目前限制单个仓库大小为 1024 MB,清除完历史记录后,还要去项目主页 Settings 下做一下 Git GC.

image-20210312193859024

原文地址:https://www.cnblogs.com/makalochen/p/14526012.html