One step to squash a branch into a single commit.
Instead of using rebase (confusing and weird sometimes) it makes a temporary branch with your then merges that with merge --squash
.
To add custom git command, saving this as git-ez-squash
(or git-whatever
) in a folder somewhere in your PATH
.
#!/usr/bin/env bash
set -e
DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
TEMP_BRANCH="temp-$(date +%s)"
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# check for uncommitted changes
if [[ -n $(git status --porcelain) ]]; then
echo "Error: There are uncommitted changes on the current branch."
exit 1
fi
git fetch origin "$DEFAULT_BRANCH"
git merge --no-ff --no-edit "$DEFAULT_BRANCH"
git checkout -b "$TEMP_BRANCH"
git branch -D "$CURRENT_BRANCH"
git checkout origin/"$DEFAULT_BRANCH"
git checkout -b "$CURRENT_BRANCH"
git merge --squash "$TEMP_BRANCH"
git commit
git branch -D "$TEMP_BRANCH"