压缩历史记录中的多个 git 提交
Squashing multiple git commits in history
我有一个包含大约 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。
执行此操作的最佳做法是什么?
假设 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 cherry-pick
范围从 git
v1.7.2 开始支持。
您也可以尝试一种类似于合并方法的不同方法。
注意: 假设 n
是最新提交,a
是最旧提交。
创建一个以提交 k
作为头的新分支。
git checkout -b new-branch <commit hash of k>
然后,将头部软重置为提交 d
的父级。
git reset --soft <commit hash of parent of d>
提交更改,因为暂存区中不存在从提交 d
到 k
的所有更改。 (git status
验证)
git commit
合并提交 post k
,
git cherry-pick l^...n # Use SHA's of l and n
我有一个包含大约 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。 执行此操作的最佳做法是什么?
假设 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 cherry-pick
范围从git
v1.7.2 开始支持。
您也可以尝试一种类似于合并方法的不同方法。
注意: 假设 n
是最新提交,a
是最旧提交。
创建一个以提交 k
作为头的新分支。
git checkout -b new-branch <commit hash of k>
然后,将头部软重置为提交 d
的父级。
git reset --soft <commit hash of parent of d>
提交更改,因为暂存区中不存在从提交 d
到 k
的所有更改。 (git status
验证)
git commit
合并提交 post k
,
git cherry-pick l^...n # Use SHA's of l and n