相关技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > 相关技巧 > git使用rebase删除某次提交

git使用rebase删除某次提交的问题

作者:java叶新东老师

文章介绍了Git删除特定commit的两种方法:通过`git rebase -i HEAD~n`交互式删除最近提交,需修改操作为`drop`并强制推送到远程;或使用`git rebase --abort`撤销操作,同时提醒处理空提交时需检查暂存区文件

git删除某次commit记录

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

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

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

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

git log

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

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

git rebase -i HEAD~1

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

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

保存并关闭文件。

Git将执行rebase操作并删除你在步骤3中指定的commit记录。

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

$ git rebase -i HEAD~1
interactive rebase in progress; onto 52784b2
Last commands done (2 commands done):
   pick abc1234 commit message 1
   drop def5678 commit message 2 (to be deleted)
Next commands to do (2 remaining commands):
   drop 46b66b5 #1000049091 xx1
   pick 2df1056 #1000027181 xxx2
You are currently rebasing branch 'feature/dubhe-pay-platform-2.12.3-bak' on '52784b2'.

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 cherry-pick --skip'
Could not apply c600123... #1000046509 门店标记二期需求

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

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

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

git add .
git commit -m "rebase"

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

git rebase --continue

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

git push origin branch_name -force
# 简写
git push origin branch_name -f

撤销 rebase 修改

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

git rebase --abort

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文