我得到它的工作使用命令git config --global core.editor
將提交列表發送到腳本而不是默認編輯器。感覺有點骯髒(也許沒有優化),但它可以工作,現在我可以在一個命令中摺疊我的倉庫的歷史!
您需要2個bash腳本。
第一個腳本replacepick.sh
,用於代替編輯器的腳本。解析所有行,並用文件(參數3)中的行#(參數2)替換(選擇1)。
#!/bin/bash
# ./replacepick.sh replacewith fromindex filename
REPLACEWITH=$1
FROMINDEX=$2
FILE=$3
echo $FILE
mathccount=0
if [ -f $FILE ]
then
OUTPUT=$FILE.tmp
echo "" > $OUTPUT
cat $FILE | while read line
do
if [[ "$line" == pick* ]]
then
matchcount=$(($matchcount + 1))
if [ $matchcount -gt $FROMINDEX ]
then
echo ${line/pick/$REPLACEWITH} >> $OUTPUT
else
echo $line >> $OUTPUT
fi
else
echo $line >> $OUTPUT
fi
done
newfilecontent=`cat $OUTPUT`
echo $newfilecontent > $FILE
fi
而且bash腳本collapsehistory.sh
調用Git命令:
#!/bin/bash
# Collapse all commits on second commit
`git config --global core.editor "/Users/macbook/Documents/GitTests/replacepick.sh squash 1"`
`git rebase -i $1`
`git rebase --continue`
# Collapse 2 commits on one
`git config --global core.editor "/Users/macbook/Documents/GitTests/replacepick.sh edit 0"`
`git rebase -i $1`
`git reset --soft HEAD^`
`git config --global core.editor "cat"`
`git commit --amend`
`git rebase --continue`
# garbage collect
`git reflog expire --expire=1.minute refs/heads/master`
`git fsck --unreachable`
`git prune`
`git gc`
# restore your favorite editor
`git config --global core.editor "vi"`
然後你執行/path/to/historycollapse.sh SHA1_OF_FIRST_COMMIT
就大功告成了!希望它也能幫助你們中的一些人。
我認爲它不再適用於最新版本的git,但我現在還沒有設法理解。 – jptsetung 2012-09-09 09:26:16