Created
May 4, 2012 02:37
-
-
Save taybin/2591528 to your computer and use it in GitHub Desktop.
update multiple branches at once
This file contains hidden or 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 | |
# from http://stackoverflow.com/questions/620650/can-i-easily-update-all-local-git-branches-from-remote-branches-simultaneously | |
main() { | |
REMOTES="$@"; | |
if [ -z "$REMOTES" ]; then | |
REMOTES=$(git remote); | |
fi | |
REMOTES=$(echo "$REMOTES" | xargs -n1 echo) | |
CLB=$(git branch -l|awk '/^\*/{print $2}'); | |
echo "$REMOTES" | while read REMOTE; do | |
git remote update $REMOTE | |
git remote show $REMOTE -n \ | |
| awk '/merges with remote/{print $5" "$1}' \ | |
| while read line; do | |
RB=$(echo "$line"|cut -f1 -d" "); | |
ARB="refs/remotes/$REMOTE/$RB"; | |
LB=$(echo "$line"|cut -f2 -d" "); | |
ALB="refs/heads/$LB"; | |
NBEHIND=$(( $(git rev-list --count $ALB..$ARB 2>/dev/null) +0)); | |
NAHEAD=$(( $(git rev-list --count $ARB..$ALB 2>/dev/null) +0)); | |
if [ "$NBEHIND" -gt 0 ]; then | |
if [ "$NAHEAD" -gt 0 ]; then | |
echo " branch $LB is $NBEHIND commit(s) behind and $NAHEAD commit(s) ahead of $REMOTE/$RB. could not be fast-forwarded"; | |
elif [ "$LB" = "$CLB" ]; then | |
echo " branch $LB was $NBEHIND commit(s) behind of $REMOTE/$RB. fast-forward merge"; | |
git merge -q $ARB; | |
else | |
echo " branch $LB was $NBEHIND commit(s) behind of $REMOTE/$RB. reseting local branch to remote"; | |
git branch -l -f $LB -t $ARB >/dev/null; | |
fi | |
fi | |
done | |
done | |
} | |
main $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment