Skip to content

Instantly share code, notes, and snippets.

@AnrDaemon
Created April 1, 2018 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnrDaemon/3a277ef91695f4f83eeb36aeceafe4e3 to your computer and use it in GitHub Desktop.
Save AnrDaemon/3a277ef91695f4f83eeb36aeceafe4e3 to your computer and use it in GitHub Desktop.
Easy and safe git squash with no manual calculations
#!/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