Jul*_*n__ 5 git git-rebase git-rewrite-history
我知道如何手动拆分提交使用git rebase -i,但是如何自动拆分文件中的每个提交?
例如,提交A修改的3个文件,f1,f2和f3.分裂后,有3个提交A-f1,A-f2和A-f3.
我想这样做是为了使重写更容易,因为我只需要压缩一些小的提交.
以下脚本HEAD按文件拆分:
#!/usr/bin/env bash
set -e
SHA=$(git rev-parse --short HEAD)
git reset HEAD^
git diff-tree --no-commit-id --name-only -r $SHA | while read -r f; do
git add "$f"
GIT_EDITOR="echo '0a\n$SHA $f\n\n.\nw' | ed -s" git commit -c $SHA
done
Run Code Online (Sandbox Code Playgroud)
生成的提交消息的格式如下:
<original SHA> <file name>
<original commit message>
Run Code Online (Sandbox Code Playgroud)
以下假定您可以在上面运行脚本git-split.
如果要按文件拆分范围内的所有提交,请使用如下所示:
git rebase --interactive --exec git-split <branch>
Run Code Online (Sandbox Code Playgroud)
如果要在交互式rebase期间拆分单个提交,请使用如下所示:
p Commit to split
x git-split
Run Code Online (Sandbox Code Playgroud)
欢迎对脚本进行任何改进.