Created
April 17, 2013 08:54
-
-
Save ludekstepan/5402828 to your computer and use it in GitHub Desktop.
Fast forward a local branch to a remote branch The local branch doesn't have to be checked out. Links:
http://stackoverflow.com/a/4157435/860556
http://stackoverflow.com/questions/5167957/is-there-a-better-way-to-find-out-if-a-local-git-branch-exists
http://stackoverflow.com/questions/3216360/git-update-a-local-branch-without-checking-it-out
This file contains 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/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