Created
March 11, 2011 06:38
-
-
Save mhl/865549 to your computer and use it in GitHub Desktop.
A script that squashes your entire branch down to a single commit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A script that squashes your entire current branch down to a single commit, | |
# if this repository has a single root commit. This will change the object | |
# name of the root commit. This is for an answer to the Stack Overflow | |
# question: http://stackoverflow.com/questions/5266340/ | |
if [ -n "$(git status --porcelain)" ] | |
then | |
echo "git status wasn't clean - refusing to run..." | |
exit 1 | |
fi | |
# From: http://stackoverflow.com/questions/1006775/ | |
root_commits () { | |
git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$" | |
} | |
NUMBER_OF_ROOT_COMMITS=$(root_commits|wc -l) | |
if (( "$NUMBER_OF_ROOT_COMMITS" > 1 )) | |
then | |
echo "This script won't work when you have multiple root commits" | |
exit 1 | |
fi | |
ROOT_COMMIT=$(root_commits) | |
if [ -z "$ROOT_COMMIT" ] | |
then | |
echo "No root commit was found!" | |
exit 1 | |
fi | |
set -e | |
set -x | |
# Create a branch based on the current HEAD for safety: | |
git branch old-master | |
# Reset the branch to the root commit, leaving the previous | |
# state of the tree staged: | |
git reset --soft $ROOT_COMMIT | |
# Now amend the root commit with the state of the index: | |
git commit --amend -m "The branch restarted" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment