From 4155e551d3086224820400bebdcc6e9c5577c856 Mon Sep 17 00:00:00 2001 From: Tony Duckles Date: Sun, 31 Dec 2017 10:32:27 -0600 Subject: [PATCH] bin/git-amend: Smarter 'git stash' handling - Fix 'stash pop' handling to always restore back to the stashed changes afterwards. - Only stash if there are local modifications. --- bin/git-amend | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bin/git-amend b/bin/git-amend index 9101a67..7369128 100755 --- a/bin/git-amend +++ b/bin/git-amend @@ -23,12 +23,15 @@ test -z "$TARGET" && { exit 1 } -# stash off work tree modifications leaving the -# index for amending to the target commit -git stash save -q --keep-index git-amend - +# if there are local work tree modifications, stash those off leaving +# the index for amending to the target commit +DIRTY=0 +if ! git diff-files --quiet --ignore-submodules --; then + DIRTY=1 +fi +test $DIRTY -eq 1 && git stash save -q --keep-index git-amend # always restore from stash before exiting -trap 'git stash pop -q stash@{git-amend} 2>/dev/null' EXIT +test $DIRTY -eq 1 && trap 'git stash pop -q stash@{0} 2>/dev/null' EXIT # go back in history git checkout -q "$TARGET" || { -- 2.47.1