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)
- 按 i 进入编辑模式(可以修改内容)
- 改成以下内容,drop 表示删除
drop 8bbfef7 提交修改
pick 066a32d 提交修改2
- 编辑完后,按 Esc 退出编辑模式,回到命令模式
- 输入 :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