Skip to content

Instantly share code, notes, and snippets.

@byroot
Created March 19, 2012 09:58
Show Gist options
  • Save byroot/2106010 to your computer and use it in GitHub Desktop.
Save byroot/2106010 to your computer and use it in GitHub Desktop.
Git squash
#!/bin/bash -e
# Go back to the last commit that we want to form the initial commit (detach HEAD)
branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
tag="tmp-$RANDOM"
git checkout HEAD
# reset the branch pointer to the initial commit,
# but leaving the index and working tree intact.
git reset --soft HEAD~1
# amend the initial tree using the tree from 'B'
git commit --amend
# temporarily tag this new initial commit
# (or you could remember the new commit sha1 manually)
git tag $tag
# go back to the original branch
git checkout $branch
# Replay all the commits after B onto the new initial commit
git rebase --onto $tag HEAD
# remove the temporary tag
git tag -d $tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment