Skip to content

Instantly share code, notes, and snippets.

@ludekstepan
Created April 17, 2013 08:54
Show Gist options
  • Save ludekstepan/5402828 to your computer and use it in GitHub Desktop.
Save ludekstepan/5402828 to your computer and use it in GitHub Desktop.
#!/bin/bash -e
# DECLARE FUNCTIONS
# branch_exists_local <branch>
function branch_exists_local {
git show-ref --verify --quiet "refs/heads/$1"
}
# branch_exists_remote <remote_branch>
# where <remote_branch> is in form: "remote/branch"
function branch_exists_remote {
git show-ref --verify --quiet "refs/remotes/$1"
}
function is_fast_forward {
if [[ `git merge-base $1 $2` = `git rev-parse $1` ]]; then
return 0;
else
return 1;
fi;
}
# PARSE INPUT ARGUMENTS
BRANCH=$1
if [[ -z $BRANCH ]]; then
echo No branch specified
exit 1
fi
if [[ -z "$2" ]]; then
REMOTE="origin/$BRANCH"
else
REMOTE=$2
fi
# RUN
if ! branch_exists_local $BRANCH; then
echo "Local branch $BRANCH doesn't exist!"
exit 1;
fi
if ! branch_exists_remote $REMOTE; then
echo "Remote branch $REMOTE doesn't exist!"
exit 1;
fi
if is_fast_forward $BRANCH $REMOTE; then
git branch -f $BRANCH $REMOTE
echo Fast-forward update performed - $BRANCH is now on $REMOTE
else
echo Cannot update $BRANCH using $REMOTE - non-fast-forward update
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment