Gitlab 删除某个提交(git bash操作)

打开 git-bash.exe

在 git bash 中输入

cd 路径/到/你的项目

查看提交历史

git log --oneline

示例输出:

066a32d (HEAD -> 分支测试, origin/分支测试) 提交修改2 
8bbfef7 提交修改
fd73fb7 合并
da6f950 提交
e3b394a 提交
7ab50ec Merge branch '分支测试' into 'main'
b8fb0c7 合并分支
1bff735 测试分支添加Closed
4cb63fb 主分支添加Load
0f4ab1b 提交修改
624eced 回退
5141613 提交分支 回退
3982cb2 提交分支
3aa0aa3 提交全部
9845482 Initial commit

删除 8bbfef7 提交修改

删除倒数第二个提交:

git rebase -i HEAD~2

假如有以下报错:

fatal: It seems that there is already a rebase-merge directory, and
I wonder if you are in the middle of another rebase.  If that is the
case, please try
        git rebase (--continue | --abort | --skip)
If that is not the case, please
        rm -fr ".git/rebase-merge"
and run me again.  I am stopping in case you still have something
valuable there.

之前运行过一次 git rebase,但没有完成或中断了,所以现在 Git 不允许你开始新的 rebase

不需要上次的 rebase,直接放弃并重新开始(推荐):

git rebase --abort

然后再执行"删除倒数第二个提交"

git rebase -i HEAD~2

弹出 Vim 窗体(还是 git bash,标题显示 MINGW64)

  1. 按 i 进入编辑模式(可以修改内容)
  2. 改成以下内容,drop 表示删除
drop 8bbfef7 提交修改
pick 066a32d 提交修改2
  1. 编辑完后,按 Esc 退出编辑模式,回到命令模式
  2. 输入 :wq (代表写入保存并退出),然后按回车
:wq

如果有以下提示:

interactive rebase in progress; onto fd73fb7
Last commands done (3 commands done):
   drop 8bbfef7 提交修改
   pick 066a32d 提交修改2
  (see more in file .git/rebase-merge/done)
No commands remaining.
You are currently rebasing branch '分支测试' on 'fd73fb7'.
  (all conflicts fixed: run "git rebase --continue")

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git rebase --skip'
Could not apply 066a32d... 提交修改2

意思是:这个提交其实在变基的过程中已经没有实际的更改内容了(也许它的内容已经包含在其它提交或被你删除的提交中)
跳过空提交

git rebase --skip

使用强制推送(force push)覆盖远程分支历史

git push origin 分支名 --force
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可能是这样吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值