首页 > 解决方案 > 如何从最新提交创建分支

问题描述

最近我遇到了一个独特的问题,我必须在一个分支中只包含最新的提交,而丢弃所有旧的。

例如考虑我有这样一个提交结构:

[main branch]
af474e3 (HEAD -> main) Updated modal UI
c0f9599 Added modal UI                    <-- This commit onwards is what our requirement is
e50c805 Updated button UI
de2ab3c Added button UI
9b1822f Initial commit

(我知道这听起来很傻,但是,)我想从那时起获取所有(最新的)提交以及与之相关的所有提交信息,即提交日期、提交作者等。

创建的分支必须如下所示:

[new branch]
af474e3 (HEAD -> new, master) Updated modal UI
c0f9599 Added modal UI

我试过removing/squashing commits with interactive rebasing了,没用。 Cherry-picking也没有用。

这可以实现吗?有没有简单的方法来实现这一目标?

标签: gitgithubversion-controlbranchconventional-commits

解决方案


您希望分支以具有类似修订c0f9599的文件和内容的修订开始吗?

git checkout --orphan new-branch c0f9599
git commit -C c0f9599 # use the same comment as c0f9599
git cherry-pick af474e3

给你!(当然,修订 ID 会有所不同)


推荐阅读