删除特定提交之前的提交

发布于 2024-09-05 17:41:58 字数 37 浏览 3 评论 0原文

有没有办法删除指定提交之前的所有提交并将该提交用作初始提交?

Is there a way to remove all commits before a specified commit and use that commit as the initial?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隱形的亼 2024-09-12 17:41:58

假设新的最旧提交的哈希值是 X,我们可以暂时使用“oldroot”和“newroot”:

git checkout -b oldroot X
TREE=`git write-tree`
COMMIT=`echo "Killed history" | git commit-tree "$TREE"`
git checkout -b newroot "$COMMIT"
git rebase --onto newroot oldroot master
# repeat for other branches than master that should use the new initial commit
git checkout master
git branch -D oldroot
git branch -D newroot
git gc # WARNING: if everything's done right, this will actually delete your history from the repo!

这将创建一个“newroot”提交,其内容与“oldroot”提交相同,但没有任何父级。然后,它将所有其他分支重新设置为新根,这应该存在于所有其他分支的历史中。

编辑:测试并修复;稍晚一点,精炼一点

Let's say the new oldest commit's hash is X and we can use "oldroot" and "newroot" temporarily:

git checkout -b oldroot X
TREE=`git write-tree`
COMMIT=`echo "Killed history" | git commit-tree "$TREE"`
git checkout -b newroot "$COMMIT"
git rebase --onto newroot oldroot master
# repeat for other branches than master that should use the new initial commit
git checkout master
git branch -D oldroot
git branch -D newroot
git gc # WARNING: if everything's done right, this will actually delete your history from the repo!

That will create a 'newroot' commit with the same contents as the 'oldroot' commit, but without any parents. Then, it rebases all the other branches onto the new root, which should be in the history of all of them.

EDIT: tested and fixed; slightly later, refined a bit

回眸一笑 2024-09-12 17:41:58

更简单的解决方案是,
考虑最初你的分支有提交ma​​in,并且你做了一个提交first,现在你还在first<之上做了一个提交second /强>。所以你有这样的情况:

main->first->second

现在你希望将 second 放在 ma​​in 之上,而不是放在 first 之上。例如:

main->second->first or main->second

您可以简单地执行以下操作,

git rebase -i main

这将为您提供一个交互式 shell,您可以在其中重新排列提交的顺序或删除您选择的任何提交。

The more easier solution would be,
Consider originally your branch has commit main and you did a commit first, now you also did a commit second on top of first. So you have something like:

main->first->second

Now you want to have second on top of main rather than on top of first. Something like:

main->second->first or main->second

You can simply do,

git rebase -i main

This will give you an interactive shell where you can rearrange the order of commits or remove any commit of your choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文