Script for creating a Git repository on a Synology NAS from Mingw32 git bash

I’ve been making several small reusable chunks of code that I need in various combinations for a few different projects coming up over the next few months.

I don’t use any source control while I’m fathoming things out as the whole layout of the code can change every few hours. The most I do is a zip backup at the end of each day. But once the code starts to settle down then I get itchy if I haven’t got it in Git. So over the last two weeks I’ve created about 5 different Git repo’s, and what was a manual process became tedious.

I have a Synology NAS (DS210+) which I love. It’s got two 1Tb drives in a Raid 1 config (mirroring). It comes with ssh and I’ve installed Git on it which makes it an ideal in-house ‘remote’ repo.

So I wanted (yet again) to go from having a local directory full of code, to having it all in both local and remote Git repo’s all linked up nicely for push and pull. I’ve done this so many times in so many ways but never settled on a single ‘process’ and always end up googling to iron out wrinkles. So this time I scripted it up.

I run this from a Git Bash shell on my windows development machine. Works a treat so far, haven’t found any issues. I’m putting it here for my own future reference really – although others might find it a useful start for doing their own. I welcome any and all comments – it’s not perfect, but it does do what I need so far – all suggestions and improvements much appreciated though 🙂


# Check the arguments
if [ "$#" -ne 2 ]
    echo ""
    echo "Usage: `basename $0` <reponame> <fromdir>"
    echo ""
    echo " reponame = the name of the repository to create"
    echo " fromdir = the directory from which to copy the source files"
    echo ""
    echo " example:"
    echo " `basename $0` supersouce.git experiment/supersource"
    echo ""
    exit 1

# some basic config settings and naming the parameters nicely
# REPONAME is the name of the git repo to create locally and on GITHOST
# FROMDIR is the directory from which initial source files are recursively copied
# GITHOST is the remote machine (synology NAS in my case) on which to create the remote repo
# GITBASE is the directory on GITHOST in which we will create the remote git repo

# derived variables
# GITPATH is the complete unix path on GITHOST of our repo
# RMT_BLD_FILE is the name of a script we create locally and scp to GITHOST and then exec via ssh

echo "$0 making local repository [$REPONAME] and remote on host [$GITHOST] in [$GITPATH]"
echo ""

echo "Building script file to execute on [$GITHOST]"

echo "#!/bin/sh" > $RMT_BLD_FILE
echo "cd $GITBASE" >> $RMT_BLD_FILE
echo "mkdir $REPONAME" >> $RMT_BLD_FILE
echo "cd $REPONAME" >> $RMT_BLD_FILE
echo "git init --bare" >> $RMT_BLD_FILE
echo "cd .." >> $RMT_BLD_FILE
echo "chown -R root $REPONAME" >> $RMT_BLD_FILE
echo "chgrp -R grp_dw_staff $REPONAME" >> $RMT_BLD_FILE
echo "chmod -R 775 $REPONAME" >> $RMT_BLD_FILE

echo "Copying script to $GITHOST"

scp $RMT_BLD_FILE root@${GITHOST}:~/

echo "Executing script to $GITHOST"

ssh root@${GITHOST} $SSHCMD

echo "Now creating local repository..."

git init
cd ..

echo "Coping 'from' source files into our local repo ready for checking in"


echo "Add these files into local repo"

git add .
git commit -m"Initial commit"

echo "Set the [$GITHOST] repo as remote to this one"

git remote add origin ssh://${GITHOST}${GITPATH}

echo "Push all the files up to remote repo"

git push --all origin

echo "Set the default branch for pulling"

git branch --set-upstream master origin/master

echo "Quick summary of where we got to"

git remote show -n origin
git status

Or you can download it from here. Hope it helps 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s