【教程】如何清空一个 Git 分支的全部 Commits

不少人有一个大胆的想法,如何清空某个分支里面全部的 commit 呢?还有一个场景,不熟悉 Git 的程序员门新建分支基于某个分支建立的,可是可能这个分支的历史 Commits 是不须要的。那么,下面我就说一下如何将分支的历史 Commits 清空吧!git

新建一个空白分支

首先,你应该切换到你须要清空的分支,而后执行 (咱们拟定为 test 吧):程序员

git checkout --orphan null_branch

而后你会发现,你分支下的全部文件都成了待添加状态,咱们能够直接执行 git add -A 添加,而后先存在 null_branchgit commit -am "Init commit."shell

删除旧本地分支

git branch -D test
删除是为了将 null_branch 重命名为以前的分支名称

而后执行重命名为以前的分支名称:this

git branch -m test

进行强制提交到远程仓库

git push -f origin test

可能遇到的问题

若是你使用过 GitHub 或者 SourceTree 可能会遇到:code

This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/pre-push.

这样的错误,很简单,执行 rm -rf .git/hooks/pre-push 删除这个 hook 便可。rem

相关文章
相关标签/搜索