Before starting, Let us know the part of What is a git-server?
A git-server is server where we can host git repositories, for e.g. GitHub & GitLab, etc.. The git platform is a decentralized version control system, which means we have the local copy of the repo, and we can externally host the repo at any place. This increases the usability for the user to even code the project even the server is offline, and can upload the changes once hit online. Setting-Up a private git-server is a good option for a private organizations like companies, or can be a nice project for a college student.
Setting-Up the linux Machine
You can use any flavour of linux distro. Make sure you setup every-thing you need to set a basic server, like Opening the required ports and stuff, accessing your server via SSH or Display. You can do this by the guidance from any developer or just tutorials.
SSH port is must for the git. So make sure to open the 22 port. You can do this with ufw
command
$ ufw enable # To start the ufw
$ ufw allow OpenSSH # To Open the 22 port
# To check if done 👍?
$ ufw status
You should see an output saying Port 22 is enabled.
Setting-Up the user for handling git server
You need to add a new user named git
on your linux machine, make sure that you add that user to sudoers file. After that, you need to access the user git
using some ssh keys. For that You need to generate ssh-keys on your local machine and copy the public keys to access your repo via SSH from your private server.
Steps for setting-up a user for git-server
-
Create a sudo user named git
$ sudo useradd git $ sudo usermod -aG sudo git $ su git # change to git user
-
Create a
.ssh
folder & store the public keys for the repositories$ mkdir ~/.ssh $ touch ~/.ssh/authorized_keys
Generate your ssh-keys on your local machine to access your repo on server. Copy the public key and paste it in this file. -
Create the /git/repo-name or /repo-name file in the file-system.
$ sudo chown git /git or /repo-name $ cd /git $ mkdir test_repo.git
By default /git folder belongs to root only. Change the Owner-Ship to git usingchown
-
Initialize the git repository.
$ git init --bare
Always use the --bare flag, because this makes the repository convenient for pulling from & pushing to. The normal repositories doesn't help pulling or pushing. -
Completed
$ git clone git@your-server-ip:/git/test_repo.git
You can use this repo just like the one cloned from your github account
That's it, your private git-server is ready to server now.
Top comments (0)