Skip to content

Instantly share code, notes, and snippets.

@selfup
Last active April 17, 2020 16:18
Show Gist options
  • Save selfup/2570bca9ef11b7c4cfcf9c27646a1876 to your computer and use it in GitHub Desktop.
Save selfup/2570bca9ef11b7c4cfcf9c27646a1876 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
if [[ $1 == '' ]]
then
echo 'please provide a branch name to savagely squash from'
echo 'exiting'
exit 1
fi
latest_rebase_target_branch_commit=$(git log $1 --format=format:%H | sed -n 1p)
latest_and_historical_commits=$(git log --format=format:%H)
depth=0
depth_check=1
for commit in $latest_and_historical_commits
do
if [[ $commit == $latest_rebase_target_branch_commit ]]
break
then
((depth++))
fi
done
if [[ $depth -eq $depth_check ]]
then
echo 'you are only one commit ahead'
echo 'amending existing commit instead..'
else
soft_reset_depth=$(($depth - $depth_check))
if [[ $soft_reset_depth < 1 ]]
then
echo 'no changes compared to $1 branch'
echo 'exiting..'
exit 1
fi
echo "soft reseting back by $soft_reset_depth commits"
git reset -soft HEAD~$soft_reset_depth
fi
git commit --all --amend --no-edit
echo "$depth comit(s) amended - history is now altered"
echo 'you may now push with force'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment