#!/bin/bash # Target Branch # Defaults to HEAD giturltarget=${1:-HEAD} giturlreference="the current branch" if [ "$giturltarget" != "HEAD" ]; then giturlreference=$giturltarget fi # Current rev: giturlcurrev=`git rev-list --max-count=1 $giturltarget` # Find all remote branches that have the current rev as their tip for giturlremote in `git remote` do giturlhasrev=`git ls-remote $giturlremote | grep "$giturlcurrev.*refs/heads/" | sed 's|.*refs/heads/||'` if [ "$giturlhasrev" ]; then echo "For sharing $giturlreference via the remote repo $giturlremote:" echo "" giturlremoteurl=`git config -l | grep ${giturlremote}.url | cut -f2 -d= | sed 's/\\([^@]*@\\|[^:]*:\\/\\/\\)\\([^:\\/]*\\)[:\\/]\\(.*\\)/\\2:\\3/'` if [ "$giturlremote" == "origin" ]; then giturlremote=remote_name echo "Change remote_name as appropriate in the below commands." echo "" fi echo "You need only add the remote once, regardless of how many branches you wish to look at." echo "To add this repo as $giturlremote:" echo " Read-only (git protocol):" echo " git remote add $giturlremote git://`echo $giturlremoteurl | sed 's/:/\\//'`" echo " Read-write (ssh protocol):" echo " git remote add $giturlremote git@$giturlremoteurl" echo "" echo "Once you have the remote added you can check out this branch:" giturlshowor= for giturlbranch in $giturlhasrev do if [ "$giturlshowor" ]; then echo "or" fi giturlshortbranch=`echo $giturlbranch | sed 's|.*/||'` echo "git checkout -b $giturlshortbranch $giturlremote/$giturlbranch" giturlshowor=yes done echo "" fi done