(i) 每位作者一次提交
我想这在逻辑上是不可能的:假设你有一个这样的提交序列:
- 提交:A,作者:Alpha
- 提交:B,作者:Beta
- 提交:C,作者:Alpha
如果提交 C 依赖于 B 中所做的任何事情,则您不能再重新排序和压缩 A 和 C。
(ii) 只有最后的代码行的原始提交
为此,您可以使用“git filter-branch --tree-filter”。请注意以下脚本可能会吃掉小猫,因为我只在一个简单的测试存储库上对其进行了测试。您已被警告:
git filter-branch --prune-empty --tree-filter '
# directory which contains the final version of the project
FINAL_VERSION="$DIRECTORY_WITH_REPOSITORY_OF_FINAL_VERSION"
# directory which contains the filtered version of the repository
FILTER_DIR="$(pwd)"
# apply the current commit in to the final version in reverse mode,
# ignore the rejects
cd "$FINAL_VERSION"
git show "$GIT_COMMIT" > /tmp/original.patch
cat /tmp/original.patch | patch -p1 -t
git diff > /tmp/filtered.patch
# reset the FINAL_VERSION to the original state.
git reset --hard
git clean -f -d -x
# apply the patch which contains the lines which could be reversed on
# the filtered version
cd "$FILTER_DIR"
# revert the last commit
patch -p1 -t < /tmp/original.patch
# apply the filtered patch
patch -p1 -t < /tmp/filtered.patch
# remove the rejects by the modified patch
find -name "*.orig" -o -name "*.rej" | xargs rm -rf
' previousRelease..HEAD
(这假设您已经用“previousRelease”标记了分支点。您还必须调整 FINAL_VERSION 变量。)