【问题标题】:Squashing about 6k commits into one将大约 6k 个提交压缩为一个
【发布时间】:2020-03-05 14:26:51
【问题描述】:

我有一个网站,当我开始开发它时,我已经克隆了 Laravel github repo 及其所有提交。现在我想删除所有原始提交,只留下我的。有没有办法做到这一点?我尝试了 git rebase,但它需要手动选择所有 6k 提交并从最新的提交中删除。 (我需要从最老的开始)。

【问题讨论】:

    标签: git


    【解决方案1】:

    我认为您可以通过检查特定提交、在该提交处创建一个孤立分支,然后重新定位到该分支来做到这一点。

    1. 在第一次提交之前检查提交
      git checkout <commit hash before your first commit>
    2. 创建一个孤立分支。索引将包含处于该状态的文件,但不会有提交历史记录。
      git checkout --orphan foo
    3. 提交压缩的更改
      git commit -m 'Squashed!'
    4. 检查您的主分支
      git checkout <original branch name>
    5. 变基到新的压缩分支。
      git rebase foo

    我在 master 上使用 5 次提交的存储库对此进行了测试:

    * 40f82e6 - 5
    * 78deb2c - 4
    * aff34f8 - 3
    * 15cd469 - 2
    * 6e420cb - 1
    

    让我们把前三个压扁(输出略):

    $ git checkout aff34f8
    Note: checking out 'aff34f8'.
    HEAD is now at aff34f8 3
    
    $ git checkout --orphan foo
    Switched to a new branch 'foo'
    
    $ git commit -m 'Squashed!'
    [foo (root-commit) 933b62d] Squashed!
    
    $ git checkout master
    Switched to branch 'master'
    
    $ git rebase foo
    First, rewinding head to replay your work on top of it...
    Applying: 1
    Using index info to reconstruct a base tree...
    Falling back to patching base and 3-way merge...
    No changes -- Patch already applied.
    Applying: 2
    Using index info to reconstruct a base tree...
    Falling back to patching base and 3-way merge...
    No changes -- Patch already applied.
    Applying: 3
    Using index info to reconstruct a base tree...
    Falling back to patching base and 3-way merge...
    No changes -- Patch already applied.
    Applying: 4
    Applying: 5
    

    更改后,master 现在看起来像这样:

    * 5d908c9 - 5
    * 477ee53 - 4
    * 933b62d - Squashed!
    

    【讨论】:

    • 我看到它可以工作,但是有很多 rebase 冲突
    【解决方案2】:

    如果您愿意丢失提交,您可以创建一个新的孤立分支。

    在本例中,分支将被称为“新鲜”:

    git checkout --orphan fresh
    

    在this question 中阅读有关git checkout --orphan 的更多信息。

    【讨论】:

    • 谢谢,但我想保留我的提交
    【解决方案3】:

    你可以使用git rebase,但是有6k左右的commits to squash,这可能很麻烦。

    另一种选择可能是执行以下操作:

    1. 从您的最终提交中,创建一个新分支:git checkout -b my-branch

    2. “重置”您的暂存文件,使其成为第一次提交和当前状态之间的差异:git reset --soft [hash of initial commit]

    3. 现在,您的暂存文件是初始提交和最后一次提交之间的所有差异。发送git commit 以提交这些更改。

    现在,在这个新创建的分支上,您在初始之后有一个提交,其中包含对那些原始 6k 提交的所有更改。

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2017-10-30
      • 2010-12-12
      • 2012-12-18
      • 1970-01-01
      • 2020-11-15
      • 2011-07-15
      相关资源
      最近更新 更多