DEV Community

Cover image for Setup Local Git Server on your system
Hemant Mishra
Hemant Mishra

Posted on

Setup Local Git Server on your system

Have you ever wonder how Github, Bitbucket are storing our git based project? Can we do these on our system? Let's find out.

So, the answer is Yes, we can create a local server on our system which behave exactly like popular version control software. Before starting I am assuming you know what Git is and used for.

Create a server repository

Whenever you clone or initialize a git repository, there is two things which can be seen one is .git folder and other is your working area.

Alt Text

.git folder is the index folder which stores the metadata of working tree, config files, hooks, head refs etc. and other is the actual working area where you can see your local files.
To know more about .git folder. Follow this link

As, we are creating server so we don't need a working area. Can we do this? Of course.
First we create a server repository named server.git, you can name anything.

mkdir server.git
cd server.git
Enter fullscreen mode Exit fullscreen mode

Now, initialize this repository with Git. But wait there is a change in the command. We have to use --bare argument with init command. What it does? It create a bare repository without any working directory. Make sense

git init --bare
Enter fullscreen mode Exit fullscreen mode

Alt Text

As soon as you initialized the repo, you got the directory url which you can use in different directory as remote url. Isn't it so easy?

Create a Client repository

Now it's time to create a client repository and check whether our setup is working or not.

mkdir client && cd client
git init
Enter fullscreen mode Exit fullscreen mode

Now, add our server as a remote to the repository.

git remote add origin /<path>/server.git/
Enter fullscreen mode Exit fullscreen mode

Alt Text

Let's check whether we're able to push our changes to the remote. Create a file with some content and add & commit.
Now push that file to the remote branch.
Alt Text

Last step, can we clone this repository or download our changes.

git clone <path_to_your_local_server>
Enter fullscreen mode Exit fullscreen mode

Alt Text
Voila! It works

What to do next?

Of course, you can host your local server repository over the internet using nginx or Apache. There are lot of things to do with Git. Comment your way to host local setup over internet.

Top comments (2)

Collapse
 
brandonwallace profile image
brandon_wallace

You can easily set up a headless Linux server inside of your house and install Git on it to set this up. That way you can do backups, set up RAID1 in case of disk failure, and use multiple computers to push and pull from your local Git server. If you have your Git server on the same computer that you use for development and the hard drive fails you will loose everything.

Collapse
 
hmnt39 profile image
Hemant Mishra

Yes, You're right. This is not the right way to save your work. But it may be the start of that one.