I started by using the git config --global core.editor to send a list of commits to a script instead of the default editor. It feels a little dirty (and maybe not optimized), but it works, and now I can ruin the history of my repositories within one command!
You need 2 bash scripts.
First, the script replacepick.sh , the script that was used instead of the editor. Passes all lines and replaces "pick" with (argument 1) from line # (argument 2) in the file (argument 3).
#!/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
And the bash script collapsehistory.sh calls the git commands:
#!/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"`
Then you execute /path/to/historycollapse.sh SHA1_OF_FIRST_COMMIT and you're done! Hope this helps some of you.
source share