我有大约 20000 次提交的 SVN 历史记录。我搬到了 Git 并保留了历史。现在我有 26000 个提交,我想将所有提交从 1 压缩到 20000。
- 初始提交:a4f5d18
- 迁移到 git:5a42d81
- 头:933eff
我尝试检查 20000 并重新设置:
git checkout 5a42d81
git rebase squash a4f5d18
但我得到:
fatal: Needed a single revision
invalid upstream squash
我有大约 20000 次提交的 SVN 历史记录。我搬到了 Git 并保留了历史。现在我有 26000 个提交,我想将所有提交从 1 压缩到 20000。
我尝试检查 20000 并重新设置:
git checkout 5a42d81
git rebase squash a4f5d18
但我得到:
fatal: Needed a single revision
invalid upstream squash
您要保留多少次提交,在下面的命令中指定该数量
git rebase -i HEAD~6000
或更准确地说
git rebase -i a4f5d18
保留下面的 6000 提交(作为选择)并将其余部分更改pick为squash
如果变基成功
git log
您可以看到您的所有提交都压缩为一个提交
最好将更改推送到远程,否则当您再次拉取时,您将看到所有提交。
git push -f
如果我理解正确,您的主分支如下所示:
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 的更全面的解释。
在所有三种情况下(交互式变基、软重置、变基),我都遇到了空格问题。Git有嫁接分支的可能,发展成replace功能:
git replace -f 5a42d81 a4f5d18
git filter-branch