我对VonC的脚本进行了重新设计,使其可以自动完成所有操作,而不要求我做任何事情。您给它两个提交SHA1,它将把它们之间的所有内容压缩到一个名为“压缩历史”的提交中:#!/bin/sh# Go back to the last commit that we want# to form the initial commit (detach HEAD)git checkout $2# reset the branch pointer to the initial commit (= $1),# but leaving the index and working tree intact.git reset --soft $1# amend the initial tree using the tree from $2git commit --amend -m "squashed history"# remember the new commit sha1TARGET=`git rev-list HEAD --max-count=1`# go back to the original branch (assume master for this example)git checkout master# Replay all the commits after $2 onto the new initial commitgit rebase --onto $TARGET $2