Introduction

There are a few things that I have to do regularly in “git”, but have a gap of a few weeks between each time I do them. This gap is just long enough me to not bother remembering them, so I find myself googling these things again and again – so here’s the first of my regular but infrequent things I have to do in git…

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.

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 🙂

#!/bin/sh# Check the argumentsif["$#"-ne2]thenecho""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""exit1fi## some basic config settings and naming the parameters nicely## REPONAME is the name of the git repo to create locally and on GITHOSTREPONAME=$1# FROMDIR is the directory from which initial source files are recursively copiedFROMDIR=$2# GITHOST is the remote machine (synology NAS in my case) on which to create the remote repoGITHOST=nas2
# GITBASE is the directory on GITHOST in which we will create the remote git repoGITBASE=/dw_git/## derived variables## GITPATH is the complete unix path on GITHOST of our repoGITPATH=${GITBASE}${REPONAME}# RMT_BLD_FILE is the name of a script we create locally and scp to GITHOST and then exec via sshRMT_BLD_FILE=build-${REPONAME}.sh
#---------------------------------------------------------------------------------------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_FILEecho"cd $GITBASE">>$RMT_BLD_FILEecho"mkdir $REPONAME">>$RMT_BLD_FILEecho"cd $REPONAME">>$RMT_BLD_FILEecho"git init --bare">>$RMT_BLD_FILEecho"cd ..">>$RMT_BLD_FILEecho"chown -R root $REPONAME">>$RMT_BLD_FILEecho"chgrp -R grp_dw_staff $REPONAME">>$RMT_BLD_FILEecho"chmod -R 775 $REPONAME">>$RMT_BLD_FILE#---------------------------------------------------------------------------------------echo"Copying script to $GITHOST"
scp $RMT_BLD_FILE root@${GITHOST}:~/#---------------------------------------------------------------------------------------echo"Executing script to $GITHOST"SSHCMD="chmod +x $RMT_BLD_FILE ; ./$RMT_BLD_FILE"
ssh root@${GITHOST}$SSHCMD#---------------------------------------------------------------------------------------echo"Now creating local repository..."
mkdir $REPONAMEcd$REPONAME
git init
cd..#---------------------------------------------------------------------------------------echo"Coping 'from' source files into our local repo ready for checking in"
cp -R $FROMDIR/*$REPONAMEcd$REPONAME#---------------------------------------------------------------------------------------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