I use a combination of these 2 articles to collapse my Git history : Collapsing a git repository's history and Combine the first two commits of a Git repository?
This is the result
git rebase -i THE_SHA_1_OF_FIRST_COMMIT
# change "pick" into "squash" for all commits except first one
# :wq (if your editor is vi)
git rebase -i THE_SHA_1_OF_FIRST_COMMIT
# change "pick" into "edit"
# :wq (if your editor is vi)
git reset --soft HEAD^
git commit --amend
git rebase --continue
# garbage collect
git reflog expire --expire=1.minute refs/heads/master
git fsck --unreachable
git prune
git gc
This work great but as I use this quite often, I wish to automatize this in a script.
The problem is some commands like “git rebase -i” open a file to edit and I have to do it manually (same problem for “git commit –amend”). Is there a way to automatize this process? I’m not asking for a solution ready to use, just the idea.
I got it working using the command
git config --global core.editorto send the list of commits to a script instead of the default editor. Feels a bit dirty (and maybe not optimized) but it works, and now I can collapse the history of my repositories within one command!You need 2 bash scripts.
First script
replacepick.sh, the script that’s used instead of the editor. Parses all lines and replace ‘pick’ by (argument 1) from line #(argument 2) in file (argument 3).And the bash script
collapsehistory.shcalling the git commands :Then you execute
/path/to/historycollapse.sh SHA1_OF_FIRST_COMMITand you’re done! Hope it helps some of you too.