git-rbranch
Managing remote git branches is a pain in the rump – I can never remember the sequence (and have no desire to). So, I created the following small script to handle it automagically.
#!/bin/sh
# git-rbranch
if [ $# -ne 1 ]; then
echo 1>&2 Usage: $0 branch_name
exit 127
fi
branch_name=$1
check_return() {
if [ $1 -ne 0 ] ; then
exit $1
fi
}
if [ -n "$(git branch |egrep "^[[:space:]*]*${branch_name}\$")" ] ; then
echo "Local branch already exists, cannot continue"
exit 1
fi
if [ -n "$(git branch -r |egrep "^[[:space:]*]*${branch_name}\$")" ] ; then
echo "Remote branch already exists, checking out"
git checkout --track -b ${branch_name} origin/${branch_name}
check_return $?
git pull
check_return $?
exit 0
fi
git push origin origin:refs/heads/${branch_name}
check_return $?
git fetch origin
check_return $?
git checkout --track -b ${branch_name} origin/${branch_name}
check_return $?
git pull
check_return $?
Simply place it on your path somewhere as git-rbranch (ie – /usr/local/bin/git-rbranch) and call with:
git rbranch <branch_name>
This will create the new remote branch, check it out and pull the changes, the only caveat is that the branch can’t exist locally.
git-info
I also find Duane Johnson’s git-info script useful for quickly grabbing the vital stats for a repo:
#!/bin/bash
# author: Duane Johnson
# email: duane.johnson AT gmail.com
# date: 2008 Jun 12
# license: MIT
#
# Based on discussion at http://kerneltrap.org/mailarchive/git/2007/11/12/406496
pushd . >/dev/null
# Find base of git directory
while [ ! -d .git ] && [ ! `pwd` = "/" ]; do cd ..; done
# Show various information about this git directory
if [ -d .git ]; then
echo "== Remote URL: `git remote -v`"
echo "== Remote Branches: "
git branch -r
echo
echo "== Local Branches:"
git branch
echo
echo "== Configuration (.git/config)"
cat .git/config
echo
echo "== Most Recent Commit"
PAGER=/usr/bin/less git log --max-count=1
echo
# echo "Type 'git log' for more commits, or 'git show' for full commit details."
else
echo "Not a git repository."
fi
popd >/dev/null
Place it on your path as git-info and call with:
git info
git-keep
Finally, a quicky to ensure empty directories make it into git by placing blank .keep files in them. This will not be to everyone’s taste, but it works when you need it.
#!/bin/sh
dir=$1
if [ -z "$dir" ] ; then
dir=.
fi
for dir in `find $dir -type d -empty |grep -v '\.git' |grep -v '\.svn' |grep -v '.hg' ` ; do
touch "${dir}"/.keep
git add "${dir}"/.keep
done
Place on the path as git-keep and call with:
git keep <dir>
Where <dir> is the top-level directory you want to store dirs from. If omitted, will use the current dir.
Download
You can download the files here:
git-rbranch
git-info
git-keep