学习笔记|git丢弃、删除已经提交的commit

假设一个分支中某个commit想要被删除掉,但是又不想通过revert的方式来实现。

commit A commit B commit C

假设想要让commit B消失,可以这样做:
首先取得想要删除的commit的前一个commit_id,记为commit_c_id; 通过rebase回到这个commit,再将不需要的commit drop掉,重新推送到server即可。
git rebase -i commit_c_id

改命令执行后会在编辑器中出现以下文档:
pick 590ebe0 Build in version number at compile time. pick c3f3fbe metadata: fix compile warnings pick 409abe5 mdns_external: move failure messages to debug level pick 4e32e15 Lowercase characters for md5 calculation of authentication digest pick 0b31ce4 tinysvcmdns: Only append .local suffix if missing. pick 9c8d605 Update PKGBUILD pick 3e41d5a Rename scripts/PKGBUILD to scripts/arch/PKGBUILD pick 50abbc4 Update and rename scripts/shairport.service to scripts/arch/shairport.service pick 33791a6 Create shairport.install pick 0fd8220 Add .install file to PKGBUILD pick e36b0d7 Update PKGBUILD pick c3d7f72 Add shairport group/user creation pick 7057d3c Run shairport as user/group shairport (non-root) pick a7393f6 Bump pkgrel pick ee1eb91 Fix PKGBUILD error pick 4264902 Forgot to close a string pick 3be3598 Fix errors pick 2806823 add user shairport to audio group pick 75e4bd1 add audio as supplementary group pick 2056217 Improve variable quoting pick 3f7ef6c Remove references to /etc/systemd/system/shairport.service.d/ pick d65b8e8 Mark as unmaintained.# 变基 348c73d..d65b8e8 到 348c73d(22 个提交) # # 命令: # p, pick = 使用提交 # r, reword = 使用提交,但修改提交说明 # e, edit = 使用提交,但停止以便进行提交修补 # s, squash = 使用提交,但和前一个版本融合 # f, fixup = 类似于 "squash",但丢弃提交说明日志 # x, exec = 使用 shell 运行命令(此行剩余部分) # d, drop = 删除提交 # # 这些行可以被重新排序;它们会被从上至下地执行。 # If you remove a line here THAT COMMIT WILL BE LOST. ...

注意看文档给出的提示,最后一行写了:
If you remove a line here THAT COMMIT WILL BE LOST.
【学习笔记|git丢弃、删除已经提交的commit】其中pick表示rebase之后需要合并的commit,如果将pick命令注释掉,那么这个pick命令对应的commit就会被删除。当不需要某个commit,将其整行使用#注释保存退出即可。最后推送代码。
git push origin t_dev --force

    推荐阅读