From f9e512cb560be09784bbc9ffb69cb088ba7cb2b7 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Tue, 25 Aug 2015 17:29:36 -0700 Subject: [PATCH] don't change branches unnecessarily --- git-update | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/git-update b/git-update index bab10a2..2809b68 100755 --- a/git-update +++ b/git-update @@ -2,7 +2,11 @@ set -e # bail on errors -ORIG_BRANCH=$(git rev-parse --abbrev-ref HEAD) +function current_branch() { + git rev-parse --abbrev-ref HEAD +} + +ORIG_BRANCH=$(current_branch) REMOTE="${1:-$(git config --get branch.${ORIG_BRANCH}.remote)}" if [[ "$ORIG_BRANCH" = "HEAD" ]]; then echo "Cannot update in a detached HEAD state" @@ -11,7 +15,9 @@ fi if [[ -z "$2" ]]; then BRANCHES="$ORIG_BRANCH" else - shift + if [[ "$1" = "$REMOTE" ]]; then + shift + fi BRANCHES="$@" fi @@ -26,11 +32,15 @@ git fetch --prune --tags "$REMOTE" for BRANCH in $BRANCHES; do echo "* Updating $BRANCH from $REMOTE/$BRANCH" - git checkout "$BRANCH" + if [[ "$BRANCH" != "$(current_branch)" ]]; then + git checkout "$BRANCH" + fi git rebase "$REMOTE/$BRANCH" done -git checkout "$ORIG_BRANCH" +if [[ "$ORIG_BRANCH" != "$(current_branch)" ]]; then + git checkout "$ORIG_BRANCH" +fi if [[ $POP_STASH -eq 1 ]]; then git stash pop