【问题标题】:Squashing multiple git commits in history压缩历史中的多个 git 提交
【发布时间】:2017-01-09 09:41:04
【问题描述】:

我有一个包含大约 3000 个提交的存储库,我想在两个日期之间的历史中间压缩大约 250 个提交,如下所示:

a--b--c--d--e--f--g--h--i--j--k--l--m--n

a--b--c--d'-------------------k--l--m--n

我已经知道 d 和 j 的日期和 shas。 这样做的最佳做法是什么?

【问题讨论】:

    标签: git rebase squash


    【解决方案1】:

    您也可以尝试一种类似于合并方法的不同方法。

    注意:假设n 是最新的提交,a 是最旧的提交。

    以提交 k 为头创建一个新分支。

    git checkout -b new-branch <commit hash of k>
    

    然后,将头部软重置为提交 d 的父级。

    git reset --soft <commit hash of parent of d>
    

    提交更改,因为从提交 dk 的所有更改都不存在于暂存区域中。 (git status验证)

    git commit
    

    合并提交帖子k

    git cherry-pick l^...n # Use SHA's of l and n
    

    【讨论】:

    • 这就是我想要的。谢谢
    【解决方案2】:

    假设a 是最旧的,n 是最新的提交:

    git rebase方法:

        git rebase <sha_of_d> -i
        # replace *pick* with *squash* for commits e..j
        # save and quit
    

    git merge方法:

        git reset --hard <sha_of_d>
        git merge --squash <sha_of_k>
        git commit
        git cherry-pick <sha_of_l>^..<sha_of_n>
    

    请注意:

    • git reset --hard 将丢弃所有未提交的更改。

    • git v1.7.2 开始支持git cherry-pick 范围。

    【讨论】:

    • 也许你是对的,但找不到更好的方法。
    • 另外,如果您使用vim,请使用::%s/pick/squash/g 将所有pick 替换为squash'es。
    • 谢谢。这可行,但提示我输入 squash 进行 250 次提交。我可以通过一系列提交,而其他提交被选中时必须完成一个壁球?
    • 很遗憾,没有,git rebase 适用于从 HEAD 开始的提交。
    • 另一种选择是使用git merge --squash,但这更痛苦,IMO。
    猜你喜欢
    • 2011-01-19
    • 2022-07-21
    • 2021-04-25
    • 1970-01-01
    • 2012-12-18
    • 2016-03-11
    • 2014-09-13
    • 2018-11-05
    • 2013-01-23
    相关资源
    最近更新 更多