删除 git 中的多个旧提交

发布于 2025-01-11 07:31:53 字数 505 浏览 0 评论 0原文

假设我有一个包含一堆提交的分支,有没有一种方法可以删除其中几乎所有的提交,除了最后 2 或 3 个提交?

例如,我的分支上有这个:

+ Commit1
+ Commit2
+ Commit3
+ Commit4
+ Commit5
+ Commit6
+ Commit7
+ Commit8
+ Commit9
+ Commit10
+ Commit11
+ Commit12
+ Commit13
+ Commit14
+ Commit15
+ Commit16
+ Commit17
+ Commit18
+ Commit19
+ Commit20
+ Commit21
+ Commit22
+ Commit23
+ Commit24
+ Commit25
+ Commit26
+ Commit27
+ Commit28

我只想保留 Commit26、Commit 27Commit28 (可以这么说,是最近的)

有没有办法可以这样做吗?

Let's say I have a branch with a bunch of commits in it, it's there a way where I can delete almost all of the commits in there, except for the last 2 or 3 ones?

For example, I have this on my branch:

+ Commit1
+ Commit2
+ Commit3
+ Commit4
+ Commit5
+ Commit6
+ Commit7
+ Commit8
+ Commit9
+ Commit10
+ Commit11
+ Commit12
+ Commit13
+ Commit14
+ Commit15
+ Commit16
+ Commit17
+ Commit18
+ Commit19
+ Commit20
+ Commit21
+ Commit22
+ Commit23
+ Commit24
+ Commit25
+ Commit26
+ Commit27
+ Commit28

I only want to keep Commit26, Commit 27 and Commit28 (the most recent ones, so to speak)

Is there a way I can do this?

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

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

发布评论

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

评论(1

此生挚爱伱 2025-01-18 07:31:53

使用 git rebase -i

我建议在你的情况下

git rebase -i Commit0

然后你删除你不想保留的提交

你会得到类似

pick Commit28
pick Commit27
pick Commit26
pick Commit25
pick Commit24
pick Commit23
pick Commit22
pick Commit21
pick Commit20
pick Commit19
pick Commit18
pick Commit17
pick Commit16
pick Commit15
pick Commit14
pick Commit13
pick Commit12
pick Commit11
pick Commit10
pick Commit9
pick Commit8
pick Commit7
pick Commit6
pick Commit5
pick Commit4
pick Commit3
pick Commit2
pick Commit1

删除行的东西:

pick Commit28
pick Commit27
pick Commit26

I recommend to use git rebase -i

On your case

git rebase -i Commit0

Then you delete the commit you don't want to keep

You will get something like

pick Commit28
pick Commit27
pick Commit26
pick Commit25
pick Commit24
pick Commit23
pick Commit22
pick Commit21
pick Commit20
pick Commit19
pick Commit18
pick Commit17
pick Commit16
pick Commit15
pick Commit14
pick Commit13
pick Commit12
pick Commit11
pick Commit10
pick Commit9
pick Commit8
pick Commit7
pick Commit6
pick Commit5
pick Commit4
pick Commit3
pick Commit2
pick Commit1

just delete lines to get:

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