git 清理提交信息 (commits) 的常用方法

用 git 管理文件的时候,commit 多了可能发现 .git 目录非常大,甚至远超管理的文件本身。下面是一些减小 .git 文件大小的方法。

清理优化本地版本库

git gc --prune=now

清理合并不必要的commit

利用 rebase 变基,交互式选择和合并commit

git rebase -i [commit_id]

完全清除提交信息/重建版本库

!!注意提前备份数据,且不要在公共分支上这么做

基本思路1:创建一个新的 orphan 分支,代替原来的分支

使用 --orphan 命令创建新的分支时,这个新建的分支和其他分支不会有任何关系,它不会包含任何先前的提交记录或者历史记录。相当于新建了一个干净的空分支,并让该分支指向一个全新的根节点。不过这个方法只能让远程的commit清空,本地原有的 .git 还是会比较大。
git checkout --orphan <new-branch-name>
git add .
git commit -am "Initial commit"
git branch -D <old-branch-name>
git branch -m <old-branch-name>
git push -f origin <old-branch-name>

基本思路2:直接删除 .git 然后重新初始化 git

rm -rf .git
git init
git add .
git cm "first commit"
git remote add origin [your_github_repo_url]
git push -f -u origin master
作者:凌晗原文地址:https://segmentfault.com/a/1190000043665795

%s 个评论

要回复文章请先登录注册