Skip to content

Instantly share code, notes, and snippets.

@evilchili
Created February 3, 2016 19:05
Show Gist options
  • Save evilchili/c9562987f886b555ff2a to your computer and use it in GitHub Desktop.
Save evilchili/c9562987f886b555ff2a to your computer and use it in GitHub Desktop.
bash function to implement a non-destructive git rebase
# Like git-rebase -i, but without touching the index or working tree.
function git-squash() {
if [ "$1" == "" ]; then
echo "usage: git-squash <number_of_commits_to_squash>"
return
fi
# note the current HEAD commit
old_head=$(git rev-parse HEAD)
echo "Current HEAD: $old_head"
# http://stackoverflow.com/a/5201642
git reset --soft HEAD~$1 && git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
# print undo action
echo
echo "To undo this change: git reset $old_head"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment