Skip to content

Instantly share code, notes, and snippets.

@Juul
Last active May 12, 2016 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Juul/923ea5fb47d2252dfb68 to your computer and use it in GitHub Desktop.
Save Juul/923ea5fb47d2252dfb68 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Run this in an existing git-tracked directory to set up
# a remote repo on hostname.
#
# Usage: ./git_setup hostname
#
# This assumes you have ssh key-based login access as the user 'git'
# on the remote server. If this is not the case,
# change the GITUSER variable below:
GITUSER="git"
if [ "$#" -ne 1 ]; then
echo "Usage: $0 hostname" >&2
exit 1
fi
if [ ! -d ".git" ]; then
echo "Error: This is not a git repo" >&2
exit 1
fi
COMMITCOUNT=`git rev-list --count HEAD 2> /dev/null`
if [ -z "$COMMITCOUNT" ] || [ "$COMMITCOUNT" -lt 1 ]; then
echo "Error: No commits" >&2
exit 1
fi
REMOTEHOST=$1
PROJECT=${PWD##*/} # dirname (not full path just last part)
echo "Project name: ${PROJECT}"
ssh ${GITUSER}@${REMOTEHOST} 'mkdir -p ~/'$PROJECT'.git && cd ~/'$PROJECT'.git && git init --bare'
if [ "$?" -ne 0 ]; then
echo "Remote command failed"
exit 1
fi
git remote add origin ${GITUSER}@${REMOTEHOST}:${PROJECT}.git
if [ "$?" -ne 0 ]; then
echo "Add origin failed"
exit 1
fi
git push --set-upstream origin master
if [ "$?" -ne 0 ]; then
echo "Push to remote failed"
exit 1
fi
echo "git repo successfully set up on and pushed to ${REMOTEHOST}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment