[英]Git how to keep latest N commits on feature branch and delete everything
我的功能分支上总共有 44 次提交。 在最近的 24 次提交中是我的。 我需要删除剩余的旧提交。 那些由于我执行错误的 rebase 操作而出现在分支上。 我确信我的提交在 git 日志列表中是最新的并且出现在顶部。
请帮忙。
首先,在错误的 rebase 之前检查您的reflog的状态可能比尝试修复由此产生的混乱更容易。
git reflog
# spot the commit you want to go back to, then
git reset --hard <goodCommit>
# or the more cautious alternative, creating a new branch from the state to recover
git checkout -b <recovery-branch> <goodCommit>
但是,如果出于任何原因无法使用 reflog 解决方案来回答这个问题,如果您已经清楚地确定了要从分支中保留的提交,只需从预期的基础重新创建一个分支,然后挑选您的提交回到它:
git checkout -b <new-branch> <base-branch>
# with the oldest good commit you spotted on the failed branch
git cherry-pick <oldestGoodCommit>^..<failed-branch>
(注意这里的^
,它的作用是引用提交的父级)
(另外, ..
不是占位符或任何东西,它实际上是范围选择的标志A..B
意思是“除了A
上的所有内容外, B
所有内容”)
但是,如果您选择此解决方案,请为沿途的潜在冲突做好准备(这本身并不是一件坏事,但与替代方案相比可能需要付出很多努力)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.