Branching with Git – Follow up on keeping remote and local sync’d

I saw a follow up comment on the article that I built my git-create-branch function on and so I updated the code. *Thanks goes to Olivier Clavel.
Here’s the update: http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/comment-page-2/#comment-18065
Here’s my original post: http://pages.uoregon.edu/vid/2012/06/05/branching-with-git-keeping-remote-and-local-syncd/

In addition to updating the git functions used I improved the function itself by adding more feedback to the user.
Here are the new functions: git-create-branch() and git-delete-branch();

function git-create-branch(){ # git-create-branch
#!/bin/sh
#from http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/comment-page-2/#comment-18065
if [[ ! -n “$1″ ]] ; then
echo 1>&2 Usage: git-create-branch branch_name
else
#$1 => branch_namea
#vars:
GCO=”git checkout -b $1″;
GPO=”git push origin $1″;
GBRSUP=”git branch –set-upstream $1 origin/$1″;
GBR=”git branch”;
GBRR=”git branch -r”;
#actions:
echo “Adding $1”
echo “1 – Create the local branch from the current one: ‘$GCO'” > /dev/tty; $GCO
echo “2 – Push that branch to the remote: ‘$GPO'” > /dev/tty; $GPO
echo “3 – Set the upstream branch to track: ‘$GBRSUP'” > /dev/tty; $GBRSUP
echo “4 – If you need to delete the remote branch use: git-delete-branch $1 or git push origin :$1” > /dev/tty; echo -n;
echo “### Local branches ($GBR) ### “; $GBR
echo “### Remote branches ($GBRR) ### “; $GBRR
fi
}
function git-delete-branch(){ # git-delete-branch
#!/bin/sh
#modified from script and comments at http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/comment-page-2/#comment-18065
#if [ $# -ne 1 ]; then
if [[ ! -n “$1″ ]] ; then
echo 1>&2 Usage: git-delete-branch branch_name
else
#$1 -> branch_name
#vars:
GCO=”git checkout master”;
GPOD=”git push origin :$1″;
GBRD=”git branch -d $1″;
GFO=”git fetch origin”;
GPL=”git pull origin master”
GBR=”git branch”;
GBRR=”git branch -r”;
#Verbose actions:
echo “Removing $1”
echo “1 – Check out master branch: ‘$GCO'” > /dev/tty; $GCO
echo “2 – Delete remote branch: ‘$GPOD'” > /dev/tty; $GPOD
echo “3 – Delete local branch: ‘$GBRD'” > /dev/tty; $GBRD
echo “4 – fetch and pull origin: ‘$GFO; $GPL;'” > /dev/tty; $GFO; $GPL;
echo “### Local branches ($GBR) ### “; $GBR
echo “### Remote branches ($GBRR) ### “; $GBRR
fi
}

And here’s the output, in an example where I create a branch named ‘hrdev’:

$ git-create-branch hrdev
Adding hrdev
1 - Create the local branch from the current one: 'git checkout -b hrdev'
Switched to a new branch 'hrdev'
2 - Push that branch to the remote: 'git push origin hrdev'
Total 0 (delta 0), reused 0 (delta 0)
To gitosis@fadev.uoregon.edu:hr_prof_devel.git
 * [new branch]      hrdev -> hrdev
3 - Set the upstream branch to track: 'git branch --set-upstream hrdev origin/hrdev'
Branch hrdev set up to track remote branch hrdev from origin.
4 - If you need to delete the remote branch use: git-delete-branch hrdev or git push origin :hrdev
### Local branches (git branch) ###
* hrdev
  master
### Remote branches (git branch -r) ###
  origin/HEAD -> origin/master
  origin/hrdev
  origin/master

Lot’s of feedback along the way including a listing of local and remote branches for verification.
And here’s the output for deleting a branch:

$ git-delete-branch hrdev
Removing hrdev
1 - Check out master branch: 'git checkout master'
Switched to branch 'master'
2 - Delete remote branch: 'git push origin :hrdev'
To gitosis@fadev.uoregon.edu:hr_prof_devel.git
 - [deleted]         hrdev
3 - Delete local branch: 'git branch -d hrdev'
Deleted branch hrdev (was f820c11).
4 - fetch and pull origin: 'git fetch origin; git pull origin master;'
From fadev.uoregon.edu:hr_prof_devel
 * branch            master     -> FETCH_HEAD
Already up-to-date.
### Local branches (git branch) ###
* master
### Remote branches (git branch -r) ###
  origin/HEAD -> origin/master
  origin/master
hr-vid-mac:hr_prof_devel vid$

It works great and I’m happy with the cleaned up function.

Implementation and usage below is adapted from my old post on the topic.

Implementation

To add this to your .bashrc

you could edit your .bashrc file

$ vim ~/.bashrc;
:i
[ctrl+v]
:wq

Now open your terminal and run
$ source ~/.bashrc

Usage

Creating a test branch in any git tracked directory:

$ git-create-branch mybranch

You know it worked if you don’t see a bunch of warnings. Also you should see both the local and remote branches listed.

then delete it again:

$ git-delete-branch mybranch

Once again you’ll see the local and remote branches listed. ‘mybranch’ should be gone from both unless you made changes locally. In which case the (branch -d) delete command will fail as the branch was not fully merged. It will provide you with instructions for forcing the delete.

Notes about the code:

I make some assumptions that your main branch on the remote host is ‘master’ ie. ‘origin master’. The author of the original post appears to have used ‘origin’ ie. ‘origin origin’.

Leave a Comment