Created
April 1, 2018 14:11
-
-
Save AnrDaemon/3a277ef91695f4f83eeb36aeceafe4e3 to your computer and use it in GitHub Desktop.
Easy and safe git squash with no manual calculations
This file contains hidden or 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/sh | |
git status --branch --untracked-files=no --porcelain=2 | ( | |
while IFS=' .' read -r _s _b _name _value; do | |
[ "$_s" = "#" ] || break | |
if [ "$_b" = "branch" ]; then | |
case "$_name" in | |
head|upstream|ab) | |
eval "_$_name='$_value'" | |
;; | |
esac | |
fi | |
done | |
[ "$(( ${_ab%\ *} ))" = "$(( $_ab ))" ] || { | |
echo "Your copy behind $_upstream by $(( -1 * ${_ab#* } )) commit(s)! Fetch first!" | |
return 1 | |
} >&2 | |
echo "Squashing $_head..$_upstream [$_ab]" | |
git rebase -i "HEAD~$(( ${_ab%\ *} ))" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment