Git 中有某种反向挑选吗?

发布于 01-17 20:24 字数 842 浏览 2 评论 0原文

tl;dr: 如何将当前的最后几次提交保留在一个分支上,并用另一个分支的历史记录替换之前的历史记录?


我有一个分支,我想保留现有的最后 3 次提交,但用另一个分支的历史记录替换历史记录。

所以我有:

        E-F-G   :: my-branch
       /
A-B-C-D   :: main
  \
   B'-C'-D'     :: new-past

并且我想要

A-B-C-D         :: main
 \
  B'-C'-D'      :: new-past
         \
          E-F-G :: my-branch

目前我正在手动执行此操作

git checkout new-past
git pull
git checkout -b my-branch-temp
git checkout cherry-pick E
git checkout cherry-pick F
git checkout cherry-pick G
git branch -D my-branch
git branch -m my-branch

是否有与cherry-pick相反的东西,您可以在merge上插入过去的提交或某些内容或一些标志或者 rebase 会让这变得更优雅?


PS-这里的情况是我位于另一个分支的子分支上,该分支正在定期重新调整基础,并且我试图避免合并提交。

PPS-如果不存在需要解决的频繁冲突,我会尽快编写现有流程的脚本

tl;dr: How can I keep the current last couple of commits on a branch, and replace the history before that with that of another branch?


I have a branch where I want to keep the existing last 3 commits but replace the history with that of another branch.

So I have:

        E-F-G   :: my-branch
       /
A-B-C-D   :: main
  \
   B'-C'-D'     :: new-past

and I want

A-B-C-D         :: main
 \
  B'-C'-D'      :: new-past
         \
          E-F-G :: my-branch

Currently I am doing this manually

git checkout new-past
git pull
git checkout -b my-branch-temp
git checkout cherry-pick E
git checkout cherry-pick F
git checkout cherry-pick G
git branch -D my-branch
git branch -m my-branch

So is there some opposite of cherry-pick where you can insert the past commit or something or some flag on merge or rebase that would make this more elegant?


PS- The case here is I am on a child branch of another branch that is getting regularly rebased and I'm trying to avoid having merge commits.

PPS- I'd just as soon script the existing process were there not frequent conflicts that need to be resolved

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

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

发布评论

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

评论(2

追我者格杀勿论2025-01-24 20:24:01

那么是否有一些与cherry-pick相反的东西可以插入过去的提交

你有正确的想法,但你想得太多了。您可以创建一个新分支并在顶部挑选您想要的提交,而不是插入过去的提交:

git checkout -b new-past new-branch
git cherry-pick E
git cherry-pick F
git cherry-pick G

从这里,您可以删除旧分支并重命名新分支:

git branch -d my-branch
git branch -m new-branch my-branch

对于大量提交,这既麻烦又耗时幸运的是,git 提供了一个名为 rebase 的命令,它可以执行批量挑选。它看起来像这样:

git checkout my-branch
git rebase --onto new-past my-branch~3

您可以使用任何提交,例如另一个分支、标签或提交哈希,而不是 my-branch-3

So is there some opposite of cherry-pick where you can insert the past commit

You have the right idea, but you are over thinking it. Instead of inserting past commits, you can create a new branch and cherry-pick the commits you want on top:

git checkout -b new-past new-branch
git cherry-pick E
git cherry-pick F
git cherry-pick G

From here, you can delete the old branch and rename the new one:

git branch -d my-branch
git branch -m new-branch my-branch

For a large number of commits, this is cumbersome and time consuming Fortunately, git provides a command called rebase which performs batch cherry picks. It looks like this:

git checkout my-branch
git rebase --onto new-past my-branch~3

Instead of my-branch-3, you can use any commit-ish, such as another branch, tag, or commit hash.

青芜2025-01-24 20:24:01

感谢 @Hasturkun 向我指出了 rebase --onto ,这让我找到了 Git rebase --onto 概述 & 如何更改 git 中的父分支?

因此,对于问题中的示例,答案是:

git rebase --onto D' D my-branch

Thanks to @Hasturkun for pointing me towards rebase --onto which led me to Git rebase --onto an overview & How change parent branch in git?

So for the example in the question the answer is:

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