前言
本文旨在说明:
1)在本地github仓库中删除某一次commit 提交信息/历史/记录;
2)如何同步到远程github仓库(使其不显示该commit的信息)。
之所以记这个,因为我在百度上没有看到合适的解决方案,所以感觉有必要在这里记录一下。
补充:删除第一次提交(commit)
git update-ref -d HEAD
参考:如何删除git中的第一个提交? https://cloud.tencent.com/developer/ask/77445
解决方案
话不多说,直接上解决方案。(精简一点,以前都要说好些废话,说些场景之类的,以后我感觉得酌情说,看情况,没必要说一些废话)
假定现在的情况是:有10个commit,然后git log
查看commit信息:
commit-A 10月
commit-B 9月
commit-C 8月
commit-D 7月
......
commit-n 1月
我现在想删除commit-C
这个提交,但同时不影响commit-A,B,D等等其他commits。
1)运行:
git rebase -i HEAD~5
rebase指令的说明:
Rebase (show the log of the last 5 commits and delete the lines you don’t want, or reorder, or squash multiple commits in one, or do anything else you want, this is a very versatile tool)
或者运行:git rebase -i <comit-D-id>
(注意:这里得选择提交时间在commit-C之前的!所以我在此选择了commit-D的id)
2)此时会进入到一个文件中,里面有
pick comit-id xxx
pick comit-id xxx
找到commit-C的id
对应的那一行pick,直接把这一行删了就行,然后保存文件,退出即可。
3)运行git log
,此时发现commit 历史中已经没有commit-C啦
4)运行git push -f origin master
,即可同步到远程github仓库。
2 可能遇到的问题
2.1 git rebase 出现错误
1)先运行git rebase --abort
,可以抛弃掉当前的git rebase
;
2)然后运行:git rebase -i HEAD~5
2.2 备份
如果怕把本地git仓库搞坏了,可以先复制一下这个仓库,作为备份,免得追悔莫及。
3 小结
今天不小心提交错了一个commit,紧张的很,搜了很久的百度没搜出来,最后看到了 [1], 一下就解决了我的问题。遂记之笔墨。
参考文献
[1] Remove specific commit https://stackoverflow.com/questions/2938301/remove-specific-commit
搜索关键字:github delete other unverified commit
此为主要参考。
[2] Git 删除具体某个提交commit的方法 https://blog.csdn.net/nathan1987_/article/details/81605531
可是可以,但是有些地方不对。
[3] git 能不能删除commit 记录? https://www.jianshu.com/p/4877b75312d8
试了,但是我没成功。