A programmer’s code repository always needs to be hosted on a server to be safe and easy to use.

Today we’re going to talk about Git servers.

git server

1. Code Hosting Services

In general, it’s not recommended to build your own Git server, but rather to use an off-the-shelf service, known as a code hosting service. They are free.

These are external services, so I won’t go into much detail. The point of this article is not them, but to talk about what to do if you have to build your own Git server.

2. Git Server Software

The only reason to build your own Git server is if you don’t have easy access to an extranet, don’t want your code on someone else’s server, or have some customization needs.

At this point, you can choose open source Git server software.

The installation of Gogs is the easiest among these software, but the functionality is relatively weak. The more powerful the software, the more complicated the installation.

If you just want to save a copy of the code remotely and don’t care about the web interface or other features, then you don’t need to install any of the above software at all, a single command is enough.

3. SSH Transfers for Git Repositories

If you’re familiar with Git, you probably know that Git supports two transfer protocols by default: SSH and HTTP/HTTPS.

Servers generally come with SSH, which means that we can push the repository to a remote server without installing anything but SSH.

So, one command is enough. We just need to create a Git repository with the same name on the remote server, and the server is set up.

1
$ git init --bare [repository name].git

The meaning of each part of the above command is as follows.

  1. git init: initializes a Git repository.
  2. --bare: This means that you don’t need a working directory for the new repository, just a Git data directory.
  3. [repository name].git: Specify the name of the repository. For example, if the repository name is example, then you will create a Git data directory called example.git.

After you run this command, you’ll have the simplest Git server possible. Later, you can push your local code to this remote Git repository via an SSH connection.

4. Operation demonstration

In the following, I demonstrate the whole operation process.

The operation is divided into two parts, first in the remote server operation, and then in the local computer operation.

4.1. Remote server operation

The following operations are all done on the remote server, assuming you are already logged in via SSH.

The main purpose of logging in to the remote server is to create a special user that you can use to do all your Git operations. This step is not necessary, but it gives you a lot of flexibility in terms of what you can do later (for example, you can share your repository with multiple people).

1
2
3
4
$ sudo mkdir /home/git
$ sudo useradd git
$ sudo mkdir -m 700 /home/git/.ssh
$ sudo cp ~/.ssh/authorized_keys /home/git/.ssh/

The meaning of the above command is as follows.

  1. Create a new home directory /home/git for the new user.
  2. Create a new user with the user name git.
  3. Create a new SSH directory /home/git/.ssh for the new user.
  4. Copy the current user’s public key to the git user for key login.

If you are only logging in with a password and not a key, then steps 3 and 4 above are not necessary, but you need to set a password for the git user with the following command.

1
$ sudo passwd git

4.2. Local computer operation

The latter operations are done on the local computer.

Assume that the IP address of the remote server in the previous section is 192.168.1.25 and the local Git repository is called example.

1
$ ssh git@192.168.1.25 git init --bare example.git

In the above command, ssh git@192.168.1.25 indicates that you are logging in to the remote server as the git user. The latter part is a syntax for SSH, and indicates the command you run on the remote server after logging in, which is to create a new remote Git data directory, example.git.

Once this command is run, you have a Git server, and you can push your code.

1
2
3
$ cd example
$ git remote add myServer git@192.168.1.25:example.git
$ git push myServer master

The above command first goes to the local repository, adds an alias for the remote server, and then pushes the code over.

5. Another way to do it

The above example uses the git init --bare command to create a new Git data directory on the remote server. In fact, a Git data directory is a normal directory that you can copy directly from your local machine.

1
$ scp -r example/.git git@192.168.1.25:/home/git

The above command uses the scp tool to copy the .git subdirectory of your local example repository to the example.git directory on the remote server. This will also set up a Git server.

Reference: https://www.ruanyifeng.com/blog/2022/10/git-server.html