繁体   English   中英

如何从 git 的历史中压缩一系列提交?

[英]How to squash a range of commits from the history of git?

我想将多个提交压缩在一起,但它们都不是当前的负责人。 这是它的样子:

commit 7e3641f857b4fb15362c43354de5f14846a461b1 (HEAD -> superset-custom-exporter, origin/superset-custom-exporter)
Author: Aviral Srivastava
Date:   Wed Nov 4 11:41:32 2020 -0500

    adds env vars pertaining to postgresdb deployment with odh

commit bae9321ea4f78973adcc50c92a10eaebd703c7eb
Author: 
Date:   Wed Oct 21 08:55:37 2020 -0300

    Fix the security role for new logged in users

commit 2974314ca485c526ed2272f6c8bad443653765ab
Author: 
Date:   Thu Oct 15 13:39:08 2020 -0400

    Revert "wip"
    
    This reverts commit 0a93c50120b49a6ad850ee0cf8b1ab0523d50f97.

commit second_commit
Author: 
Date:   Thu Oct 15 11:50:24 2020 -0400

    wip

commit some_commit
Author: 
Date:   Thu Oct 15 10:30:47 2020 -0400

    Upgrade to superset version 0.37.2

commit some_commit
Author: 
Date:   Thu Oct 22 12:40:49 2020 -0400

    wip

commit some_commit
Author: 
Date:   Thu Oct 22 12:19:47 2020 -0400

    adds slices monitoring

commit first_commit
Author: 
Date:   Thu Oct 15 16:07:03 2020 -0400


我想将从 hash second_commit到 hash first_commit提交first_commit成一个单一的提交。 我了解如何从过去的当前范围执行此操作,但是当两个提交都在过去时,我不知道如何执行此操作。

您使用git rebase -i HEAD~8类的命令,然后将要压缩的每个提交的行上的pick更改为s

最简单的方法是进行交互式 rebase 这为您提供了一个提交列表,并允许您指定要压缩的提交。 交互式 rebase 是清理提交的好方法,同时保持其中一些提交的完整性。

git rebase --interactive HEAD~[5]

--interactive标志与git rebase一起使用来重写一系列提交,直到您提供哈希为止,例如:

git rebase --interactive 05a159

显然用你自己的 sha 替换05a159 正如杰夫·斯科特·布朗所提到的,用squashs (使用缩写)交换pick

当你以这种方式重写历史时,你正在创建一个由被压扁的对象组成的新对象,并扔掉旧对象(直到垃圾收集)。 如果您对此操作感到遗憾,请使用git refloggit reset --hard将您的分支设置为较旧的哈希,或者在开始变基之前在您的分支尖端创建一个本地标记。

实际上,要压扁您的项目,不仅应该移动头部,还应该将 master 移动到相同的位置。

要做到这一点 1.you should reset in soft >>>>>>> git reset --soft [second commit's hash value] 2.put 你修改后的提交作为下一次提交 >>git commit -am "The last commit"

现在你已经被压扁了。 实际上,使用“git log --oneline”而不是“git log”命令。 它缩短了您的文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM