git 使用 rebase 删除某次 提交

发布时间:2025-05-13 21:32:41 作者:yexindonglai@163.com 阅读(37)

git删除某次commit记录
在Git中,要删除某次commit记录有几种不同的实现方法:

方法一:使用git rebase命令和~标记

该方法适用于删除最近的几次commit记录。

首先,使用以下命令查看你需要删除的commit的记录

  1. git log

找到你要删除的commit的哈希值(commit ID)。

运行以下命令来使用git rebase命令删除commit记录(假设删除最近的一次commit)。

  1. git rebase -i HEAD~1

这将打开一个交互式的rebase窗口。找到要删除的commit记录所在的行,将其前面的pick改为drop。

  1. pick abc1234 commit message 1
  2. drop def5678 commit message 2 (to be deleted)

保存并关闭文件。
Git将执行rebase操作并删除你在步骤3中指定的commit记录。

这里出现了一个问题,看一下错误

  1. $ git rebase -i HEAD~1
  2. interactive rebase in progress; onto 52784b2
  3. Last commands done (2 commands done):
  4. pick abc1234 commit message 1
  5. drop def5678 commit message 2 (to be deleted)
  6. Next commands to do (2 remaining commands):
  7. drop 46b66b5 #1000049091 xx1
  8. pick 2df1056 #1000027181 xxx2
  9. You are currently rebasing branch 'feature/dubhe-pay-platform-2.12.3-bak' on '52784b2'.
  10. nothing to commit, working tree clean
  11. The previous cherry-pick is now empty, possibly due to conflict resolution.
  12. If you wish to commit it anyway, use:
  13. git commit --allow-empty
  14. Otherwise, please use 'git cherry-pick --skip'
  15. Could not apply c600123... #1000046509 门店标记二期需求

大致意思是 有一些提交记录中,没有任何的文件变动,问我们要不要保留这些空(无文件改动)的提交,按需选择即可,我选择的是

  1. # 跳过当前无法自动合并的提交,并继续处理后续的提交
  2. git cherry-pick --skip

看下暂存区是否有未添加的文件,如果有,使用以下命令添加到暂存区,并提交到本地仓库

  1. git add .
  2. git commit -m "rebase"

然后告诉git,已经处理完rebase了

  1. git rebase --continue

最后强制推送到远程仓库即可, 注意,一定要强制推送,否则白改了

  1. git push origin branch_name -force
  2. # 简写
  3. git push origin branch_name -f

撤销 rebase 修改

如果你在 git rebase 过程中遇到问题并希望中止 rebase,可以使用以下命令:

  1. git rebase --abort

这个命令会将你的分支恢复到 rebase 开始之前的状态,撤销所有未完成的 rebase 操作。

关键字git

上一篇: maven 打包报错 process terminated

下一篇: 没有了