code diy linux

GIT – how to setup your own server

Let’s focus on the two most important scripts for our git server, the create and the delete script:


This is the create script, as you can see it’s a bit complex because it will do a few things for me:

  • create the bare repository using the argument provided on the command line or asking for a project name
  • check before creating the repo, if another repo with the same name exists or if a directory with files in it exists. That way I’ll make sure to only create a repo inside an empty directory.
  • add a “custom-hooks” directory that will hold a link to the script as well as my script
  • create a post-receive script that uses pee from the moreutils project to run multiple scripts at the same hook.
  • ensure sane permissions on the whole project directory.
  • always assume no (the safest option) when asking the user about the action to take.

So after saving this script as “create” inside the git-shell-commands directory, we’ll give it executable permissions and we can move to the delete script.

# cat > /var/git/git-shell-commands/create
# chown git:git -R /var/git/git-shell-commands
# chmod 0755 /var/git/git-shell-commands/create


Let’s see the delete script:

This script is much simpler than the previous one, it’ll accept the name of the project as argument on the command line or will ask for it and will only delete it if it is a proper git repository, otherwise it will just exit with an error code.

I improved the way those scripts recognise a git repository from simply relying on the fact that there’s a HEAD file inside the directory they’re checking, which wasn’t the best option, to using git itself to check if the directory is a bare repository. Much better!!

Since we are here let’s modify the help command to make it show a short description of every available command.

The main thing I added is the support for a command line argument, now I’m able to run it by itself and display the usual output with a list of available commands, or followed by a command name to give a brief explanation like this:

git> help
Hi git, Run 'help' for help, 'help <command>' for specific help on a command, run 'exit' to exit. Available commands:
git> help create
usage:  create  - create a git bare repository named PROJECT.git
        this command will setup the repo and send a mail for confirmation.

Pretty nice isn’t it?! Now it’s much more user friendly, and to show the description I used awk and cut to parse the comment at the top of every script I have in the git-shell-commands directory.

In the next page we’ll see the usual routine I follow when working with this new setup.

By Danilo M.

Hi, I'm Danilo, I own this website.
I love Linux, I prefer Slackware over every other distribution, I live, eat, breath WordPress. I'm interested in technology in general and I try to write on this blog whenever I find the time..

Enjoy your stay on

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.