如何分割每个提交文件? [英] How to split every commit by file?
问题描述
我知道如何使用 git rebase -i
手动分割一个提交,但是我怎样才能通过文件自动分割每个提交?例如,提交 A
修改3个文件,f1,f2和f3。 分割后,有3个提交A-f1,A-f2和A-f3。
我想这样做,不得不挤压一些小的提交。 按照以下脚本分割 HEAD
: >#!/ bin / bash
set -e
LF = $'\''
SHA = $(git rev-parse --short HEAD )
MSG = $(git show -s --format =%B HEAD)
set -f; IFS = $'\\\
'
FILES =($(git diff-tree --no-commit-id --name-only -r HEAD))
set + f;未设置IFS
git重置HEAD ^
为$ {FILES [@]}中的f;做
git添加$ f
git commit -m$ SHA $ f $ LF $ LF $ MSG
完成
生成的提交消息的格式如下:
<原始SHA> <文件名>
<原始提交讯息>
用法
以下内容假定您可以在脚本之上运行,如 git-split
。
如果要按文件拆分范围内的所有提交,像这样使用它:
git rebase --interactive --exec git-split< branch>
如果您想在交互式重新绑定期间拆分单个提交,请像这样使用它:
p承诺分拆
x git-split
欢迎对脚本进行任何改进。
I know how to manually split a commit using git rebase -i
, but how can I automatically split every commit in a branch by file?
For instance, commit A
modified 3 files, f1, f2 and f3. After the split, there are 3 commits A-f1, A-f2 and A-f3.
I want to do this to make a major rewriting easier as I will only have to squash some small commits.
The Script
The following script splits HEAD
by file:
#!/bin/bash
set -e
LF=$'\n'
SHA=$(git rev-parse --short HEAD)
MSG=$(git show -s --format=%B HEAD)
set -f; IFS=$'\n'
FILES=($(git diff-tree --no-commit-id --name-only -r HEAD))
set +f; unset IFS
git reset HEAD^
for f in "${FILES[@]}"; do
git add "$f"
git commit -m "$SHA $f$LF$LF$MSG"
done
The generated commit messages are of the form:
<original SHA> <file name>
<original commit message>
Usage
The following assumes that you can run above script as git-split
.
If you want to split all commits in a range by file, use it like this:
git rebase --interactive --exec git-split <branch>
If you want to split a single commit during an interactive rebase, use it like this:
p Commit to split
x git-split
Any improvements to the script are welcome.
这篇关于如何分割每个提交文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!