Skip to content

Instantly share code, notes, and snippets.

@genedelisa
Created October 16, 2014 13:15
Show Gist options
  • Save genedelisa/d94f68d3a78f0055806f to your computer and use it in GitHub Desktop.
Save genedelisa/d94f68d3a78f0055806f to your computer and use it in GitHub Desktop.
Bash script to create a bare git repo then add remote to current directory
#!/usr/bin/env bash
# create a bare git repo
# Gene De Lisa
# === the variables
gitdir=~/Dropbox/git
# assumes you're in the project directory, so get the current dir name without the
# full path.
project=${PWD##*/}.git
# === end variables
usage() { echo "Usage: $0 [-d gitdir] [-p project]" 1>&2; exit 1; }
while getopts ":d:p:" o; do
case "${o}" in
d)
gitdir=${OPTARG}
;;
p)
project=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${gitdir}" ] || [ -z "${project}" ]; then
usage
fi
newrepo=$gitdir/$project
if [ -d $newrepo ]; then
while true
do
echo ""
read -n 1 -p "$newrepo exists. Continue? [Y/n/q] " answer
case $answer in
[yY] | "" )
echo ""
echo "Okay, will reinit the existing $newrepo."
break;;
[nNqQ]* ) echo ""
exit;;
* ) echo "Dude, just enter Y or N or Q, kay?";;
esac
done
fi
while true
do
echo ""
# read a single char with the prompt
read -n 1 -p "Create a bare repo at $newrepo? [Y/n/q] " answer
case $answer in
[yY] | "" )
echo ""
echo "Okay, creating $newrepo."
echo git init --bare $newrepo
git init --bare $newrepo
break;;
[nNqQ]* ) echo ""
exit;;
* ) echo "Dude, just enter Y or N or Q, kay?";;
esac
done
if [ -d .git ]; then
echo ""
echo $PWD is already a git repo. Not adding or commiting.
else
while true
do
echo ""
read -n 1 -p "git init, add, and commit [Y/n/q] " answer
case $answer in
[yY] | "" )
echo "Okay, initing, adding, commiting."
echo git init .
git init .
echo git add .
git add .
echo git commit -m \"first commit\"
git commit -m "first commit"
break;;
[nNqQ]* ) echo ""
exit;;
* ) echo "Dude, just enter Y or N or Q, kay?";;
esac
done
fi
while true
do
echo ""
read -n 1 -p "git add remote and push $project to $newrepo? [Y/n/q] " answer
case $answer in
[yY] | "" )
echo ""
echo "Ok, adding remote and pushing $project to $newrepo."
echo git remote add origin $newrepo
git remote add origin $newrepo
echo git push -u origin master
git push -u origin master
break;;
[nNqQ]* ) echo ""
exit;;
* ) echo "Dude, just enter Y or N or Q, kay?";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment