git – pushing a new local branch to remote

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…

Pushing a new local branch to remote

git push -u origin local-branch-name

which should give something like this…

Counting objects: 71, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (36/36), done.
Writing objects: 100% (37/37), 530.39 KiB | 365 KiB/s, done.
Total 37 (delta 28), reused 0 (delta 0)
To ssh://nas2/dw_git/rpd_deploy.git
 * [new branch] local-branch-name -> local-branch-name
Branch local-branch-name set up to track remote branch local-branch-name from origin.

 

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 🙂

#!/bin/sh

# Check the arguments
if [ "$#" -ne 2 ]
then
    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
fi

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

#
# derived variables
#
# GITPATH is the complete unix path on GITHOST of our repo
GITPATH=${GITBASE}${REPONAME}
# RMT_BLD_FILE is the name of a script we create locally and scp to GITHOST and then exec via ssh
RMT_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_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"

SSHCMD="chmod +x $RMT_BLD_FILE ; ./$RMT_BLD_FILE"
ssh root@${GITHOST} $SSHCMD

#---------------------------------------------------------------------------------------
echo "Now creating local repository..."

mkdir $REPONAME
cd $REPONAME
git init
cd ..

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

cp -R $FROMDIR/* $REPONAME
cd $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

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