Bash script to create git repository


While working on multiple projects, sometimes you need to deploy them somewhere. Often it is your VPS/VDS machine. When you have a lot of projects you're working on, it's a headache to create git repos for each project, and to configure it's deployment. In this article I'll show you how to automate this process using bash.

So, let’s start.

Step 1

First of all, we create variables:

  • GITREPOSDIR – directory where all git repos are stored
  • NEWREPO – directory where new repo will be stored (it consists of GITREPOSDIR and the first parameter passed to the script)
  • WEBDIR – the web directory, where all files will be copied after update git repo (I usually use git on web projects. So I need to sync my git repo with a web directory, which must be different of git repo dir)
  • GITUSER – the name of the user you will use when cloning your repo (I have created separate “git” user).

#!/bin/bash -e
# usage: ./gitreponew.sh REPO_NAME WEBDIR GIT_USER
GITREPOSDIR="/home/git/gitrepos"
NEWREPO="$GITREPOSDIR/$1"
WEBDIR="$2"
GITUSER="$3"

Step 2

Next, we check if NEWREPO dir already exists. If so – the script aborts:

if [ -d "$NEWREPO" ]; then
	echo "[FATAL]: Directory $NEWREPO already exists!"
	echo "Exiting script"
	exit 1
fi

Step 3

Next step is to create the WEBDIR directory, where all content from git repo will be copied:

if [ ! -d "$WEBDIR" ]; then
echo “Creating webdir $WEBDIR…”
mkdir -p “$WEBDIR”
fi

Step 4

When directories are created, we initialize the new git repo:

echo "Creating dir for new repo at $NEWREPO..."
mkdir -p "$NEWREPO"
cd "$NEWREPO"
git init --bare
echo "Git repo initialized!"

Step 5

Configure the post-update hook, to make git copy all files from repo to the webdir:

echo "Configuring post-update hook..."
touch "hooks/post-update"
echo "#!/bin/sh" >> "hooks/post-update"
echo "git archive --format=tar HEAD | (cd $WEBDIR && tar xf -)" >> "hooks/post-update"
chmod 755 "hooks/post-update"

Step 6

The last step is changing owner of git repo and webdir. I do it because I always log in to my vps and run the script with a different user (not "git" user). So, after creating directories we need to change the owner:

echo "Chowing dirs..."
chown "$3:$3" -R "$NEWREPO"
chown "$3:$3" -R "$WEBDIR"
echo "Done! Now you can clone your repo (don't forget to push first to master branch)"

If you are using git on non-web projects, or if you don’t need to copy files after update to your web dir – you can skip step 5. If you are using a single user for running the script and working with your repo – you can skip step 6.

To use the script, just run

./scriptName.sh newrepo /var/www/newwebdir USER.

That’s it. Below is the full version of script.

#!/bin/bash -e
# usage: ./gitreponew.sh REPO_NAME WEBDIR GIT_USER
GITREPOSDIR="/home/git/gitrepos"
NEWREPO="$GITREPOSDIR/$1"
WEBDIR="$2"
GITUSER="$3"
if [ -d "$NEWREPO" ]; then
	echo "[FATAL]: Directory $NEWREPO already exists!"
	echo "Exiting script"
	exit 1
fi
if [ ! -d "$WEBDIR" ]; then
echo “Creating webdir $WEBDIR…”
mkdir -p “$WEBDIR”
fi
echo "Creating dir for new repo at $NEWREPO..."
mkdir -p "$NEWREPO"
cd "$NEWREPO"
git init --bare
echo "Git repo initialized!"
echo "Configuring post-update hook..."
touch "hooks/post-update"
echo "#!/bin/sh" >> "hooks/post-update"
echo "git archive --format=tar HEAD | (cd $WEBDIR && tar xf -)" >> "hooks/post-update"
chmod 755 "hooks/post-update"
echo "Chowing dirs..."
chown "$3:$3" -R "$NEWREPO"
chown "$3:$3" -R "$WEBDIR"
echo "Done! Now you can clone your repo (don't forget to push first to master branch)"

About

My name is Eugene. I am a passioned web developer, in love with Node.js, with over 6yrs of experience.

My main domain of expertise is full cycle application development. I love building robust scalable Node.js applications.

I create all kind of applications - websites, API servers, realtime applications.

Skills / Technologies
Node.js, MongoDB, Mongoose, Socket.io, Mocha, Grunt, Git, LAMP, C/C++, Nginx
Contact