March 30, 2023
The Simplest Git Server Ruan Yifengs Web Log.webp

The Simplest Git Server – Ruan Yifeng’s Web Log

The reason why this post seems so long is because over 17 people write this content for our viewers. so enjoy

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

Let’s talk about Git servers today.

bg2022102304

1. Code hosting service

In general, it is not recommended to build a Git server by yourself, but to use an off-the-shelf service, that is, a code hosting service. They are all free.

Among them, except for the last Gitee, which is a domestic service, the others are foreign services.

These external services will not be introduced further. The focus of this article is not on them, but on what to do if you have to set up your own Git server.

2. Git server software

The reason for building a Git server by yourself is nothing more than inconvenient access to the external network, unwillingness to place the code on someone else’s server, or some customization requirements.

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

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

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

3. SSH transmission of Git repository

Students familiar with Git may know that Git supports two transport protocols by default: SSH and HTTP/HTTPS.

Servers generally come with SSH, which means,We can install nothing and just push the repository to the remote server via SSH.

So, one command is enough. As long as we create a Git repository with the same name on the remote server, the server is set up.


$ git init --bare [仓库名].git

In the above command, the meaning of each part is as follows.

(1)git init: Initialize a Git repository.

(2)--bare: Indicates that the new repository does not need a working directory, only the Git data directory is created.

(3)[仓库名].git: Specify the warehouse name, for example, the warehouse name isexamplethen create aexample.gitThe Git data directory.

After executing this command, a simplest Git server is born. Later, we can connect via SSH and push the local code to this remote Git repository.

4. Operation Demonstration

Below, I demonstrate the entire operation process.

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

4.1 Remote server operation

The following operations are all done on the remote server, assuming you are logged in via SSH.Students who are not familiar with SSH can refer to this articleGetting Started with SSH

The purpose of logging in to the remote server is to create a special user through which all Git operations are done. This step is actually not necessary, but it is more flexible in the later operation (for example, the warehouse can be shared by multiple people).


$ 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 user’s home directory/home/git

(2) Create a new user with the user namegit

(3) Create a new user’s SSH directory/home/git/.ssh

(4) Copy the current user’s public key togituser, so that the key can be logged in. For a detailed explanation, please refer to“SSH key login”

If you only log in with a password and do not log in with a key, then the third and fourth steps above are not required, but you need togitThe user sets the password, the command is as follows.


$ sudo passwd git

4.2 Local computer operation

The following operations are all done on the local computer.

Assume that the IP address of the remote server in the previous section is192.168.1.25the local Git repository is namedexample


$ ssh [email protected] git init --bare example.git

In the above command,ssh [email protected]means withgitAs the user, log on to the remote server.The latter part is a syntax of SSH, indicating the command executed on the remote server after logging in, that is, creating a new remote Git data directoryexample.git

After this command runs, you have a Git server, and you can push the code.


$ cd example
$ git remote add myServer [email protected]:example.git
$ git push myServer master

The above command first enters the local warehouse, adds an alias to the remote server, and then pushes the code to it.

Five, another method of operation

The above example usesgit init --barecommand to create a new Git data directory on the remote server. In fact, the Git data directory is an ordinary directory, and it can be copied directly from the local computer.


$ scp -r example/.git [email protected]:/home/git

The above command usesscptoolthe local examplein the warehouse.gitsubdirectory, copied to the directory of the remote serverexample.git. This will also create a Git server.

6. Reference link

(Finish)

Ewen Eagle

I am the founder of Urbantechstory, a Technology based blog. where you find all kinds of trending technology, gaming news, and much more.

View all posts by Ewen Eagle →

Leave a Reply

Your email address will not be published.