Make Git Local and Remote Repo

I created a shell script the turns any folder into a git repo with a remote branch master. The process is based on the quick setup described on DreamHost’s Wiki.

The script can be used to easily initialize a local folder or take an existing local repo, and make the remote branch that will be used to push commits to pull down and merge code with team members.

Usage

I assigned the alias “mkgit” to the script for easy access from any directory. It accepts two parameters: a project name and remote host.

mkgit [project-name] [user@host]

$ cd my-project-dir
$ mkgit my-project user@host.com
 
'Not a git repository. Make it a repository? (y/n)'
$ y
 
'Initialized empty Git repository in ~/my-project-dir/
[master (root-commit) 954d600] initial commit
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore'
 
'Create remote repository at ssh://user@host.com/~/git/my-project.git?
  (y/n)'
$ y
 
'Setting up remote repository at ssh://user@host.com/~/git/my-project.git'
 
'Initialized empty Git repository in ~/git/project.git/
Counting objects: 3, done.'
 
'Writing objects: 100% (3/3), 212 bytes, done.'
 
'Total 3 (delta 0), reused 0 (delta 0)
To ssh://user@host.com/~/git/my-project.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.'

mkgit.sh source

#!/bin/bash
 
# TODO: configure path via the command
REMOTE_PATH="~/git"
 
# If the current dir is not a git repo offer to make it one
if [ ! -d .git ]; then
  echo "Not a git repository. Make it a repository? (y/n)"
  read ANSWER
  if [ "$ANSWER" = "y" ]; then
    git init
    touch .gitignore
    git add .
    git commit
  else
    exit
  fi
fi
 
# Name remote repo using 1st param or current dir name
if [ -z "$1" ]; then
  PROJECT="${PWD##*/}.git"
else
  PROJECT="$1.git"
fi
if [ ! "$REMOTE_PATH" = "" ]; then
  PROJECT="$REMOTE_PATH/$PROJECT"
fi
 
# Use host specified in 2nd param or a default host
if [ -z "$2" ]; then
  HOST="user@host.com"
else
  HOST="$2"
fi
 
echo "Create remote repository at ssh://$HOST/$PROJECT? (y/n)"
read ANSWER
if [ ! "$ANSWER" = "y" ]; then
  exit
fi
 
# Create a bare remote repository
echo "Setting up remote repository at ssh://$HOST/$PROJECT"
ssh $HOST "mkdir -p $PROJECT; cd $PROJECT; git init --bare"
 
# Push to the remote repository
git remote add origin "ssh://$HOST/$PROJECT"
git push origin master git branch --set-upstream master origin/master

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>