When working with the Git version control system, you have many choices such as Github, Bitbucket, or Gitlab. If you want to avoid depending on third-party providers, you can run your own Git server.
Setting up a Git server by yourself allows you to no longer be restricted by public provider's free plans such as maximum repository size, creating unlimited private repositories, and so on.
In this context, we shall look into how to set up a Git server on your Ubuntu 20.04 machine.
How to install Git Server on Ubuntu ?
To begin with this installation, you need to update your packages list by running:
$ sudo apt update
To install Git, run the following command:
$ sudo apt install git
Verify that Git was successfully installed on your system:
$ git version
It's highly recommended that you should create a new Linux user for managing the Git repositories, run:
$ sudo useradd -m -r -U -d /home/git-repos git
The home directory of git user is located at /home/git-repos.
To increase security, we'll create an ssh key to log in to the git user.
Switch to the log-in session of git user by running:
$ sudo su - git
To create the SSH directory and file for holding the authorized ssh key for git user, run the following commands:
$ mkdir -p ~/.ssh
$ chmod 700 ~/.ssh
$ touch ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
Once, the server was successfully set-up, it’s time to create a new git repository:
$ git init --bare ~/linuxapt.git
How to configure Git repository on Ubuntu ?
Here, you have to add your local user's public SSH key to the authorized_keys file of the git user.
If you already have generated an SSH key for your local user, you can skip the following step:
$ ssh-keygen -t rsa
Now, you can retrieve your public SSH key by running:
$ cat .ssh/id_rsa.pub
This command will display the public SSH key.
So, Copy the above public SSH key then paste it into the authorized_keys file of the git user.
On your local Ubuntu 20.04 machine, assuming that you already had an unversioned directory, for example, ~/go. Change the current directory to it:
$ cd ~/go
$ git init
Next, you have to add a git remote to track your local repository on Git server:
$ git remote add origin git@git-server-ip-address:linuxapt.git
Verify that your Git server was successfully installed and configured, run the following command:
$ cd ~/go
$ touch README
$ git add .
$ git commit -m "Add file Readme"
$ git push origin master