How to set up a simple Git server by yourself?
Setting up a simple Git server can be achieved by creating a Git server based on the SSH protocol. Below are the steps for setting up a simple Git server:
- Install Git software on the server. If your server is running on a Linux system, you can directly install Git through package management tools. If it is a Windows system, you can download the installation program from the Git official website and install it.
- Create a Git user. For security reasons, it is advisable to create a user specifically for Git operations. You can use the following command to create a user named git:
sudo adduser git
- Generate an SSH key for the Git user on the server side by using the following command.
sudo su git
ssh-keygen -t rsa
- Create a bare repository. Create a bare repository in the home directory of the Git user to store project code. Use the following command to create a bare repository named myproject.git:
mkdir myproject.git
cd myproject.git
git init --bare
- Git users are allowed to connect to the server via SSH protocol. A .ssh directory should be created in the home directory of the Git user, and the SSH key should be added to the authorized_keys file to enable Git users to connect to the server via SSH. The following commands can be used for setup:
mkdir ~/.ssh
cat id_rsa.pub >> ~/.ssh/authorized_keys
- Push the local project code to the Git server by running the following command in the project directory on your local machine:
git remote add origin git@your_server_ip:myproject.git
git push -u origin master
Now, a simple Git server has been set up. You can now connect to the server via SSH and perform version management operations.