Category Archives: Git

GIT – remove unused branches

If you have some branche son your remote repository that are no longer in use you can delete them by following the steps below (make sure no one is updating these branches anymore).

First make sure you are not on the branch you want to delete

git checkout master

Then list the branches (remote and local) defined in your local repository

git branch -a

Now delete the branch with name CompanyEntity (casesensitive) from local if its (still) there

# Delete local
git branch -d CompanyEntity
# Deltete remote
git push origin --delete CompanyEntity

Now to update the branches for a repository at another location (deleting stale branches) execute the command:

git fetch --prune

The local branches will be synced with the remote branches


Setup git-http-backend on ubuntu / apache




Since version 1.6.6 GIT is able to tunnel its native protocol through HTTP or HTTPS. In this post I describe how to set things up so you can use GIT over HTTP(s). As always it is best to make use of HTTPS for security reasons. In this setup we use Basic authentication so you better use HTTP

I also use virtualmin to keep my hosting business running but that should not be a problem when following along with the steps in this post.


Ok first of all create a subdirectory in your public_html  (document root) directory. This is where we are going to store the repositories. I suggest you call this directory….git (lowercase). Change directory to your new folder.

mkdir /home/[username]/public_html/git
chown [username]:[username] /home/[username]/public_html/git
cd /home/[username]/public_html/git

Apache htaccess and htpasswd

We are going to create a couple of CGI scripts to have more control over the way the backend is executed. First create a .htaccess file with the following contents:

Options +ExecCGI
AddHandler cgi-script cgi
AuthUserFile /home/[username]/public_html/git/.htpasswd
AuthType Basic
AuthName "Git Private"
Require valid-user

This tells apache that it is ok to execute a CGI script from this folder (line 1, 2). It also tells apache to require a “valid-user”; this user can be found in the .htpasswd file (see below). Now we have to create a password file for the user authentication:

htpasswd -c .htpasswd username
New password:
Re-type password for user username

For testing purpose you could create an index.html  file and try to open that in the browser. The browser should ask your username and password now.

CGI Scripts

Now create a CGI script that will initialise a new bare repository for us to use. Create an init.cgi  script with the following contents (extend parameter checking if you wish).



# init.cgi
#       Initialise a new git repository. 
# Example:
#       https://[yourdomain]/git/init.cgi?reponame=mynewrepo
# Params:
#       reponame        - the name of the git repository to create
# Remarks
# - The name of the repository may only contain the letters a-z and A-Z
# - The repository should not exist already
echo 'Content-type: text/plain'



if [ "${#parm[@]}" -ne "2" ]; then
        echo "Invalid number of parameters"
        exit 1

if [ "${parm[0]}" != "reponame" ]; then
        echo "Invalid parameter name ${parm[0]}"
        exit 2

if ! [[ ${parm[1]} =~ ^[a-zA-Z]*$ ]]; then
        echo "Invalid parameter value ${parm[1]}"
        exit 3

if [ -d "$GIT_PROJECT_ROOT/${parm[1]}.git" ]; then
        echo "Git repository already exists"
        exit 4

mkdir -p "$GIT_PROJECT_ROOT"

git init --bare "$GIT_PROJECT_ROOT/${parm[1]}.git/" 2>&1

echo "Repository created at `date` from $REMOTE_ADDR" > "$GIT_PROJECT_ROOT/${parm[1]}.git/description"
echo "`date` : \"${parm[1]}\" repository created from ip $REMOTE_ADDR" >> "$logfile"
echo >> "$logfile"

exit 0

When you execute this script via the browser (https://yourdomain/git/init.cgi?reponame=[yourreponame] ) a new bare repository is created. The actual repositories are created in the subdirectory repos below the git folder.

The next script will startup the actual GIT http backend. I have wrapped this in an additional script so I could perform some logging. Create a script called git.cgi  in your git directory with the following contents.



# git.cgi
#       Execute the git http-backend command
# Params
#       As handed by the git client command
# Example:
#       git clone https://[yourdomain]/git/git.cgi/myrepo.git

git http-backend "$@" 2>> "$logfile" || echo failed >> "$logfile"

echo "`date` : git command executed $@" >> "$logfile"

Finally you need a little configuration script, named , which sets some general parameters. Source is shown below.

# When not setting the variable below every repo has to have the magic file
# git-daemon-export-ok. If both are not present a message "Repository not exported"
# shows iup in the log file
export GIT_PROJECT_ROOT=~/public_html/git/repos

Now you can clone a repository by sending your browser to the url


Example workflow

In your browser: https://[yourdomain]/git/init.cgi?reponame=first
In your shell (local): git clone https://[yourdomain]/git/git.cgi/git/repos/first.git
Apply your changes
Add all items to the staging area: git add –all
Commit all changes in the staging area: git commit -am “My commit message”
Push the changes back to the server: git push

That’s all; happy GITing


Windows GIT and SSH keys

gitTo use SSH keys on your windows system follow the steps below:
Install the git extensions for windows. This will (among other things) install the Git Bash shell. Execute a git bash shell. Now create your private/public key pair with the ssh-keygen command (or copy an existing key). Add a passphrase for additonal security.

On windows the ssh files are store in c:\Users\Administrator\.ssh\

Copy the public part to your git server with the ssh-copy-id command.

In Git bash edit your .profile (or create one) and add the coding snippet below:


function start_agent
   echo "Initialising new SSH agent..."
   /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
   echo succeeded
   chmod 600 ${SSH_ENV}
   . ${SSH_ENV} > /dev/null

if [ -f "${SSH_ENV}" ]; then
   . ${SSH_ENV} > /dev/null
   ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {

If you have multiple keys you should also create a config file in the .ssh folder. In this file you specifiy which key should be used for what host; like this:

 IdentityFile ~/.ssh/your_key_for_this_host

Setup GIT server

This post describes how to setup your ubuntu server as a GIT server. Clients are able to clone repositories and push their changes back to the server with this setup. We make use of ssh in combination with the git-shell to provide extra security.git

On your GIT server execute the commands below:

sudo apt-get install git
sudo adduser git
vi /etc/shells # add "/usr/bin/git-shell"
vi /etc/passwd # change shell for user git to "/usr/bin/git-shell"
touch /home/git/.hushlogin # prevent welcome message on ubuntu system
mkdir /home/git/git-shell-commands
vi /home/git/git-shell-commands/no-interactive-login # add text below
printf "\nInteractive shell access not provided; go away!\n\n"
exit 128
#end add text
chown -R git:git /home/git/git-shell-commands
chmod u+x no-interactive-login

If you use a Synology server add the following line to your /etc/shell file:

Create a bare GIT repository on the server at /opt/git:

mkdir /opt/git
mkdir /opt/git/the_project.git
cd /opt/git/the_project.git
git --bare init
chown -R git:git /opt/git

Now on your local machine create a repository  and push it to the server:

cd ~/
mkdir the_project
git init
git add .
git commit -m "Initial commit"
git remote add origin git@yourgitserver:/opt/git/the_project.git
git push origin master

To clone the repository to your local development machine execute the following commands:

git clone git@yourgitserver:/opt/git/the_project.git
# this will clone the repository into a directory "the_project".
git status # check the status of the repository
git log # check the log messages
git tag # list the tags available
git checkout v1.0.0 # check out the v1.0.0 tag
# With the "git status" command you can see that the HEAD is detached:
# HEAD detached at v1.0.0
git checkout master # back to HEAD version
# No longer a detached head message when you execute "git status":
# On branch master

When you have made changes to a file you want to undo (revert changes back to the last commit) execute the command below:

git checkout -- yourfile.txt

GIT and SSH public / private key

If you want to use git with a SSH key follow these steps. A SSH key is a convenient (and very secure!) way to login to your server. First create a public / private key pair with the command (-t is the type of encryption and -C is a comment):

ssh-keygen -t rsa -C "git @ repo machine"

Protect this key with a strong passphrase. After the key is generated you have to copy the public part to the server. Off course this should also be done in a secure way so use the ssh-copy-id tool for this. Execute the command:

ssh-copy-id -i gitkey git@yourmachinehis will copy the private key to the user home directory in a folder .ssh with the name authorized_keys.
 Now ssh to your machine with the command:
ssh git@yourmachine

The ssh_agent will ask for your passphrase (not the user password) and remember it. If you don’t have a ssh_agent enabled you could do without the passphrase but that is a bit less secure.


Working with Visual Studio 2010 and github on Synology

gitInstall the “GIT server”  package on your Synology Disk Station. Goto the package center and search for the “Git Server” package. Download and install it.

Install GitExtensions on your dev box.

Install the latest msysgit on your dev box.

Install the “Git Source Control Provider”  Visual Studio 2010 extension on your dev box. In Visual Studio goto Tools -> Extensions and search for “Git Source Control Provider” in the online gallery.

Now SSH to your Synology (either with putty or with a Linux terminal) and execute the following commands:

cd /volume1/
mkdir git
cd git
mkdir repo1.git
cd repo1.git
git --bare init
cd ..
chown -R johndoe:users repo1.git

This will create a so called “bare git” repository. A bare git repository can be used as a central repository to which you push your local repositories.

Ok; now there is a GIT repository created on your Synology. Next create a new Visual Studio solution. Right click the solution name and select “Create Git Repository”. This will add an .git folder to your solution directory and now your solution is in a local Git repository.

Next commit your new solution to the Git repository.Select all files; type your comment and hit “Commit”.

Now push your local repository to the central bare git repository you created earlier. Right click the solution name; choose Git (master) -> Push. The checklist for Git settings will show up if not all settings are valid. For now just click Ok. The Push dialog appears. Specify the remote name; for example: ssh://johndoe@

Some useful GIT commands
In Visual Studio goto GIT -> GIT bash; a Bash command prompt will start.
Show available tags:

git tag

Create a tag in the GIT bash:

git tag -a v1.0.2 -m "The new version"

Commit your changes with a message:

git commit -m "Your message / comment"

Push the tag information:

git push --tag

Delete a tag:

git tag -d name_of_the_tag

GIT Use Case

Create bare repo on diskstation

git init --bare repo.git

On a client clone (initialise) a repository from the dskstation:

git clone ssh://git@diskstation.local/volume1/git/repo.git

[Remote “origin”] can be found in .git/config file; you could add an alias for convenience. A directory repo will be created that contains your files. Add files to your repo directory

Add them to the local stage:

git add file.txt

Commit them to the local repo:

git commit -m "Your comment"

Push them to the server repo:

git push origin master

A new branch master will be created. From now on you can use git push (without the origin master)