Useful script for working with remote branches in git

Want to learn something new? Check out my most recent Egghead course:



Build a Twelve-Factor Node.js App with Docker - WATCH NOW on Egghead.io!

Cheers -Mark

Wed, 12/28/2011 - 13:31

Submitted by markoshust Wed, 12/28/2011 - 13:31
Tags: 

Working with git using remote branches is a great tool to manage workflow and coordinating code with other developers. However, the commands you need to remember could be quite daunting and even hard to remember for the experienced programmer.

I created a simple script at github called git-remotebranch. A copy of the script is below (please see github for the latest and greatest). Just chmod it 777, throw it in your /etc/init.d/ folder (or another directory which has been added to the binary shell path), and you are good to go. It's pretty self explanatory.

#!/bin/sh
# git-remotebranch 
# author: Mark Shust 

if [ -z ${2} ]; then
  echo "You must use this script in the format git-remotebranch "
  exit
fi

case $1 in
'add')
    git checkout -b ${2}
    git push -u origin ${2}
    echo "The ${2} remote branch has been added"
    ;;
'rm')
    git push origin :heads/${2}
    # also delete local branch, comment out if not needed
    git branch -D ${2}
    echo "The ${2} remote branch has been removed"
    ;;
'co')
    git checkout --track -b ${2} origin/${2}
    ;;
*)
    echo "Invalid action. Must be 'add', 'rm' or 'co'."
    ;;
esac