© 2021. All rights reserved.

Extending Git

March 18, 2014

Goal

In this tutorial I will go over how to setup a simple script to automate the creation of a remote git repository based on your current git repository and push to that remote repository over ssh.

I will assume you have some knowledge of ssh and are somewhat familiar with shell scripts. Leave a comment for any clarifications or if you find a bug.

The problem

I am a big fan of how simple it is to initialize a git repository, just two simple words, git init. However, I don't like how long it takes to create a remote repository and do the first initial push. Wouldn't it be nice if I could do something like git create and have a remote repository be created and push my current commits.

This is when I started looking into how I could "extend" git to add this desired automation. I did a quick google search and found this blog post by By Stefan Saasen. It wasn't exactly what I was looking for but it demonstrated how simple it was to "extend" git. Just crate script with the prefix git-name in any language and make sure the file is executable and in a directory that is in your $PATH environment variable.

So I created a file called git-create and started think about what I would need to create and push to a remote repository.

Checking for a git repository

Well my first issue was determining if my present working directory was in fact a git repository. To do this I used the following lovely command:

git rev-parse --show-toplevel

It will travel through the directory tree in reverse and try to find a git repository, and if it is successful, it will give us the directory name of said repository.

Setting up the remote repository

After we determine if we are in a git repository, we can proceed with setting up the remote repository. We need to ssh into our remote machine (whose address we will assume is in a variable $server) to setup a bare repository (assume we save the repository name in a shell variable $repo. We can achieve that with the following:

ssh $server "
mkdir ${repo}.git
cd $repo
git init --bare
"

Now we have a brand new remote git repository which we named after our current git repository.

Push current repository

Assuming the ssh connection was successful and all of the commands executed without error, we want to push our local copy of the repository to the server. This is also assuming that you haven't set the remote origin server for your master branch, you can run the following code:

git remote add origin $server:${repo}.git
git push --set-upstream origin master

git-create

After going through all of the pieces, hopefully this next block makes sense and is useful. There are some variables at the beginning to help you easily customize the script.

git-create
#!/bin/sh

# Simple shell script to initialize a remote repository and do and initial
# push.

user='???'      # username to use when logging in
server='??'     # address of remote server
dir='???'       # directory on the server to cd into

# calculate repository name based on current repo
repo=$(basename `git rev-parse --show-toplevel 2> /dev/null`).git


if [ $repo == '.git' ]; then
    echo 'Not a git repository.'
    exit 1
fi

# execute on server
ssh $user@$server "

cd $dir
if [ ! -d $repo ]; then
    mkdir $repo
    cd $repo
    git init --bare
fi

"

# execute on client
git remote add origin $user@$server:$dir/$repo
git push --set-upstream origin master

Conclusion

Well this script is pretty small but it will save you a lot of time when starting up a new repository; just two simple words, git create. Make sure its executable with chmod +x git-create and in a directory in your $PATH environment variable. Of course you could rename the script if you don't like the name, but it was the only one I could think of at the time.

Well I hope this tutorial was helpful and you learned a few things, and let me know if you have any questions. I am always happy to help.