[正解] 删除Github仓库某一次commit信息/历史

前言

本文旨在说明:
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
试了,但是我没成功。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值