loading...
Cover image for Setting Up Your Own Git Server

Setting Up Your Own Git Server

erhankilic profile image Erhan Kılıç ・2 min read

This post was first published on Setting Up Your Own Git Server

Setting up your own git server is very useful. You can set up your own git server on Vps, Vds, Dedicated servers or even on your local machine. I assume you have installed git on your Linux server.

First we need to create a git directory under the root directory. In the following code I will create a example named git server. So you can set up as many git servers as you like.

mkdir /git/example.git

Then in the terminal, go to the directory /git/example.git and write the following code.

git init --bare

That’s all!! We created our git server. If you want to clone you should write the following code;

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

It will ask your server’s password.

If you have a local project on your computer and you want to include it in your git server, type the following code in the terminal under your project directory;

git remote add myserver username@serveripaddress:/git/example.git
git push myserver -u master

You can change “myserver” with a name whatever you want. “origin” is commonly used name.

When you send your commits with the push command, your server will ask for the user password.

If you want your server to take your commits and create your project in another directory when you send your commits, you must create a post-recived named file under the /git/example.git/hooks directory with 777 permission. why would you want such a thing? In your project, we assume that you have commands that do build operations like gulp. After sending your commits, you can want that your gulp processes run automatically and the your project will be ready. You can even have development and production branches run automatically in separate folders.

Edit the inside of post-recived file as follows.

#!/bin/bash
GIT_WORK_TREE=/home/project-directory/ git checkout -f master

You can set the directory location as you like.

If you are using something like composer, bower, you can arrange them to work as follows.

#!/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

Posted on by:

erhankilic profile

Erhan Kılıç

@erhankilic

I’m Erhan Kılıç, a curious web developer from Turkey who loves to write software applications and websites. I have been working as a web developer since 2013.

Discussion

markdown guide
 

Setting up my own git server is something I've been meaning to do for several years, but never got around to it, thinking that it was a lot more complicated than this.

I could not believe how simple and easy it is!

Thank you for posting.

 
 

If you don't plan to let other people contribute to your repositories, git might be overkill.

 

If Git would be overkill here, what would you propose as an alternative? I use Git on every project I work on, whether I'm the only one working on it or not, and it's saved my bacon more times than I can count!

 

Have you tried Darcs? It is perfect for small projects - and it won't cause problems when you try to merge multiple incompatible features.

I have! I used to use it primarily a long time ago, but then I switched to using Git for my personal stuff when my job converted their repositories to Git - easier to keep one tool in my head! I still miss Darcs' UI - best VCS UI I've ever used.

You should try again! :-)

Not everything is a nail just because you have a hammer.

 

Is there a way when I push my changes to my server and the server will create/update on another directory that I can access?

 

Yes you can do it and it's on article. You can edit post-recived and write bash code.

 

Yeah. This could be considered part of a CI/CD pipeline. It usually needs some other tools set up on top of Git.

 

Are there any alternatives to setting permissions to 777? I try to avoid this as a matter of principle.

 

Yes but you must generate ssh key for your user. You can read detailed information on git book.

 

Why not just use services like GitHub or Bitbucket?

 

Some people or companies wants to use their own git server. Some of them doesn't trust to GitHub, Bitbucket, Gitlab etc... I worked at some companies that uses their own git server. They customized everything for their purpose. I don't need to use my own git server but I learned when I work with these companies and I wanted to share my knowledge.