Git 中有某种反向挑选吗?
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 技术交流群。

发布评论
评论(2)
感谢 @Hasturkun 向我指出了 rebase --onto
,这让我找到了 Git rebase --onto
概述 & 如何更改 git 中的父分支?
因此,对于问题中的示例,答案是:
git rebase --onto D' D my-branch
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
你有正确的想法,但你想得太多了。您可以创建一个新分支并在顶部挑选您想要的提交,而不是插入过去的提交:
从这里,您可以删除旧分支并重命名新分支:
对于大量提交,这既麻烦又耗时幸运的是,git 提供了一个名为
rebase
的命令,它可以执行批量挑选。它看起来像这样:您可以使用任何提交,例如另一个分支、标签或提交哈希,而不是
my-branch-3
。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:
From here, you can delete the old branch and rename the new one:
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:Instead of
my-branch-3
, you can use any commit-ish, such as another branch, tag, or commit hash.