Example of Hosting Your Own Git Server with Shell

Setting up a personal git server is possible on various platforms such as Vps, Vds, Dedicated servers, or even your local machine. If using Windows, the git-scm tool can facilitate the establishment of a git client and provide a graphical user interface for git repositories.

The initial release of the article was on the topic of establishing your personal Git server.

Establishing your personal Git server can prove to be beneficial as it can be set up on various platforms such as Vps, Vds, Dedicated servers or even your local machine. Assuming that you have installed Git on your Linux server.

To begin with, a git directory should be established within the root directory. The given code will illustrate the creation of a sample git server, which means you have the option to set up multiple git servers.

mkdir /git/example.git
Enter fullscreen mode
Exit fullscreen mode

Navigate to the directory /git/example.git in the terminal and input the following code.

git init --bare
Enter fullscreen mode
Exit fullscreen mode

Great work! Our Git server has been successfully created. To clone the server, simply use the following code.

git clone username@serveripaddress:/git/example.git

Enter fullscreen mode
Exit fullscreen mode

It will ask your server’s password.

To add a local project from your computer to your git server, navigate to your project directory in the terminal and enter the following code.

git remote add myserver username@serveripaddress:/git/example.git
git push myserver -u master
Enter fullscreen mode
Exit fullscreen mode

The name of “myserver” can be replaced with any preferred name of your choice. Typically, “origin” is a frequently used name for this purpose.

Upon pushing your commits, the user password will be requested by the server.

To have your server create your project in a different directory upon receiving your commits, you need to create a file named post-received in the hooks directory of /git/example.git with 777 permission. This is useful if your project has build commands, such as gulp, that need to run automatically after committing. With this setup, you can have separate folders for development and production branches to run automatically.

Alter the content within the received file labeled “post” in the following manner.

#!/bin/bash
GIT_WORK_TREE=/home/project-directory/ git checkout -f master
Enter fullscreen mode
Exit fullscreen mode

You have the flexibility to choose your preferred directory location.

You can configure tools such as composer or bower to function in the following manner.

#!/bin/bash
GIT_WORK_TREE=/home/project-directory/ git checkout -f master
cd /home/project-directory/
php composer.phar update
cd /home/project-directory/
bower install
Enter fullscreen mode
Exit fullscreen mode

Frequently Asked Questions

Posted in Git