Skip to content

Instantly share code, notes, and snippets.

@urschrei
Created July 31, 2010 19:03
Show Gist options
  • Save urschrei/502501 to your computer and use it in GitHub Desktop.
Save urschrei/502501 to your computer and use it in GitHub Desktop.
Automatically link a dir and contents with a Github repo of the same name
#!/bin/bash
#######################################################################################
## save as "github" in your path, and chmod+x
## Takes one optional argument: a directory name (new or existing)
## in which to create the repo (script assumes it's the same name as the Github repo)
## if called without an argument, assumes you wish to use the current working directory
## example: "github foo" creates (if it doesn't exist) dir foo in current working dir
## switches to foo initialises git repo, adds files, performs initial commit,
## sets remote master to git@github.com:github_user/foo.git and
## attempts to push to it. If the push fails, .git dir is removed and the script exits
## with code 1
## substitute your github username for git_user, on line 14
########################################################################################
user=urschrei
ex=`pwd`'/'$1
# This will give us the full path plus a new directory name, if it was passed
bs=`basename $ex`
# this will give us just the directory name
OIFS=$IFS
IFS='.' read -ra trimmed <<< "$bs"
IFS=$OIFS
remotename=${trimmed[0]}".git"
# trim the basename in case it's got a .extension
if [ ! -d "$ex" ]; then
echo "Directory" $ex "does not exist, creating…"
mkdir $ex
newdir=yes
# only do that stuff if the directory doesn't exist
fi
if [ ! "$(ls -A $ex)" ]; then
echo "Directory is empty, creating a blank readme…"
touch $ex'/'readme
# create a blank readme file if the dir is empty, so we'll have something to push
fi
cd $ex
git init
git add .
git commit -m "Initial commit"
git remote add origin git@github.com:$user/$remotename
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
git push origin master
if [ $? -eq 0 ]; then
echo -e '\n'
echo "Successfully created and linked local and Github repos for" $bs
echo "Would you like to open this project in TextMate? (y/n): "; read yn
if yn=="y"; then
open -a TextMate $ex
exit 0
fi
exit 0
else
echo -e '\n'
if [ -n "${newdir+x}" ]; then
echo "*** ERROR: could not push to specified Github repo. Rolling back directory creation and exiting. ***"
rm -rf $ex
exit 1
# only remove directories created by this script
fi
echo "*** ERROR: could not push to specified Github repo. Rolling back git init and exiting. ***"
rm -rf .git
rm -f readme
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment