压制多个历史提交

时间:2017-08-10 09:18:07

标签: git git-squash

我有大约20000次提交的SVN历史记录。我搬到Git并保留了历史。现在我处于26000次提交,我想要将所有提交从1到20000压缩。

  • 初始提交:a4f5d18
  • 迁移到git:5a42d81
  • HEAD:933eff

我试过检查20000并重新定位:

git checkout 5a42d81
git rebase squash a4f5d18

但我明白了:

fatal: Needed a single revision
invalid upstream squash

3 个答案:

答案 0 :(得分:2)

您希望保留多少次提交,请在以下命令中指定该号码

git rebase -i HEAD~6000

或更准确地说

git rebase -i a4f5d18

从下方保留6000提交(作为选择)并将pick的其余部分更改为squash

如果rebase成功

 git log

您可以看到所有提交压缩为一次提交

最好将更改推送到远程,否则当您再次拉动时,您将看到所有提交。

git push -f

答案 1 :(得分:1)

如果我理解正确,你的主分支看起来像:

master: 1 -> 2 -> 3 -> .. -> 20000 -> A (First non migrated commit) -> B -> C -> ..

你想去:

master: 1' (All your migrated commits) -> A' -> B' -> C' -> ..

我认为您可以遵循使用git rebase HEAD~26000 (first commit hash probably easier)并将pick更改为squash的方法,但这可能非常耗费时间。

一个可行的解决方案是使用您的第一个20000的内容创建一个新的提交。可能值得在备份分支上进行测试。

git checkout <last migrated commit hash> -b backup-master

backup-master: 1 -> 2 -> 3 -> .. -> [20000] -> A (First non migrated commit) -> B -> C -> ..
                                       ^- you are here.

git reset --soft <first migrated commit hash>

backup-master: [1] -> 2 -> 3 -> .. -> 20000 -> A (First non migrated commit) -> B -> C -> ..
                ^- you are here         ^- the working directory tree/index reflects this commit 

修改您的初始提交内容/消息(如果您愿意,可以创建新提交)。

git commit --amend

现在backup-master应该包含您的压缩迁移提交,让我们移动新的提交。

git checkout master

git checkout -b master-rebase(万一我们搞砸了)。

git rebase backup-master - 我相信这会有效,因为git知道成功重组所需的合并。如果这样做,您应该完成,master-rebase将包含您想要的结果。

如果失败,您可能会使用rebase --onto.

取得更好的成功

git rebase --onto <destination commit> <parent of first desired commit> <last desired commit>

即。 git rebase --onto backup-master <A>~1大师`

如果这样可行,它会将您置于目前任何分支上的提交中,因此您需要创建一个:

git checkout -b rebase-success

可以找到关于rebase --onto的更全面的解释here

答案 2 :(得分:0)

在所有三种情况下(交互式rebase,软重置,rebase到),我遇到了空白问题。 Git有可能移植分支,它发展成替换功能:

git replace -f 5a42d81 a4f5d18
git filter-branch
相关问题