DEV Community 👩‍💻👨‍💻

Cover image for Setting Up Your Own Git Server
Erhan Kılıç
Erhan Kılıç

Posted on

Setting Up Your Own Git Server

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
Enter fullscreen mode Exit fullscreen mode

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

git init --bare
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (15)

Collapse
 
mediumrowdy profile image
medium-rowdy

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.

Collapse
 
tux0r profile image
tux0r

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

Collapse
 
hoelzro profile image
Rob Hoelz

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!

Collapse
 
tux0r profile image
tux0r

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

Thread Thread
 
hoelzro profile image
Rob Hoelz

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.

Thread Thread
 
tux0r profile image
tux0r

You should try again! :-)

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

Collapse
 
lukad profile image
Luka Dornhecker • Edited on

Or you could just install gitolite.

Collapse
 
foresthoffman profile image
Forest Hoffman
Collapse
 
arcaz21 profile image
Arcaz21 • Edited on

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?

Collapse
 
quinncuatro profile image
Henry Quinn

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

Collapse
 
erhankilic profile image
Erhan Kılıç

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

Collapse
 
gabrielbhh profile image
Gabriel Ben Harosh

Why not just use services like GitHub or Bitbucket?

Collapse
 
erhankilic profile image
Erhan Kılıç

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.

Collapse
 
erhankilic profile image
Erhan Kılıç

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

Forem

🏗 We built a 100% open source community software called Forem.

 
You can contribute to the codebase or host your own.