Skip to content

Instantly share code, notes, and snippets.

@scottalan
Last active March 9, 2017 13:57
Show Gist options
  • Save scottalan/698a13af6e0315c66178227c2ad89975 to your computer and use it in GitHub Desktop.
Save scottalan/698a13af6e0315c66178227c2ad89975 to your computer and use it in GitHub Desktop.
Git: squash & rebase
# @arg int: The number of commits you want to squash.
function squash() {
num=${1};
if [ ! num ]; then
echo "Integer required";
return 0;
fi
git reset --soft HEAD~${num};
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})";
}
# @arg string: The branch you are applying your changes "on top of".
function rebase() {
rebase_on=${1};
status=`git status --porcelain`
if [ -z ${status} ]; then
branch=$(git symbolic-ref --short -q HEAD)
git checkout ${rebase_on}
git pull
git checkout ${branch}
git rebase ${rebase_on}
else
echo "Commit your changes: ${status}"
return 0;
fi
}
@pmrbb
Copy link

pmrbb commented Mar 9, 2017

Great functions, however please change the name of the variable 'status' in the rebase function to avoid an error with zsh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment