YouTip LogoYouTip

Git Server

Setting up a Git Server | Rookie Tutorial

Setting up a Git Server | Rookie Tutorial

In the previous chapter, we used GitHub as our remote repository. Public repositories on GitHub are free, and starting in 2019, private repositories on GitHub can also be used without restrictions.

Of course, we can also set up our own Git server to serve as a private repository.

Using a Bare Repository

1. Install Git

Install Git on an Ubuntu server:

sudo apt install git

If you are using CentOS/RedHat, the installation command is:

yum -y install git-core

Fedora installation command:

# yum install git (Fedora 21 and earlier versions)
# dnf install git (Fedora 22 and newer versions)

Next, we create a git user group and user to run the Git service:

$ groupadd git
$ useradd git -g git

2. Create a Bare Repository

Log in as the Git user and create a bare repository in their home directory.

$ sudo su - git

First, choose a directory to serve as the Git repository. Assume it is /home/gitrepo/.git. In the /home/gitrepo directory, run the following commands:

$ cd /home
$ mkdir gitrepo
$ chown git:git gitrepo/
$ cd gitrepo
$ git init --bare .git

The above commands create an empty repository. Git repositories on servers usually end with .git. Then, change the ownership of the repository to the git user (if operated by another user such as root):

$ chown -R git:git .git

3. Set Up Certificate-Based Login

Add your public key to ~/.ssh/authorized_keys to allow remote access.

Collect the public keys of all users who need to log in. Public keys are located in the id_rsa.pub file. Import your public key into the /home/git/.ssh/authorized_keys file, one per line.

If the file does not exist, create it:

$ cd /home/git/
$ mkdir .ssh
$ chmod 755 .ssh
$ touch .ssh/authorized_keys
$ chmod 644 .ssh/authorized_keys
# Add your SSH public key to the file

4. Clone the Repository

$ git clone git@192.168.45.4:/home/gitrepo/.git
Cloning into ''...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

192.168.45.4 is the IP address of the server hosting Git; you need to replace it with your own Git server IP.

This completes our Git server setup.


Using GitLab

GitLab is a powerful Git service management tool suitable for medium to large teams, offering rich features such as user management, CI/CD, and code review.

1. Install GitLab

Install GitLab according to the official GitLab documentation.

For example, on Ubuntu:

# sudo apt-get update
# sudo apt-get install -y curl openssh-server ca-certificates tzdata perl
# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash
# sudo EXTERNAL_URL="http://yourdomain" apt-get install gitlab-ee

Set EXTERNAL_URL="http://yourdomain" to your own domain name or public IP address, for example:

sudo EXTERNAL_URL=101.132.XX.XX yum install -y gitlab-ee

2. Configure GitLab

After installation, open your browser and visit http://yourdomain to set up the administrator account.

When you see a response similar to the following, GitLab has been successfully installed.

Image 1

3. Create a Project

Log in to GitLab and create a new project with the username root.

Retrieve the login password:

sudo cat /etc/gitlab/initial_root_password

The result will look like this:

Image 2

Use the username root for the first login:

Image 3

4. Generate Key Pair and Retrieve Public Key

Install Git tools (skip if already installed):

sudo apt-get install git

Generate the key pair file id_rsa:

ssh-keygen

During key generation, the system will prompt you to enter the directory to store the key pair (default is .ssh/id_rsa under the current user's home directory, e.g., /home/test/.ssh/id_rsa) and a passphrase for the key pair. You can manually enter these or press Enter to accept the defaults.

After generation, you'll see output similar to the following:

Image 4

View and copy the content of the public key file id_rsa.pub:

cat .ssh/id_rsa.pub

You'll see output similar to the following:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQVwWjF3KXmI549jDI0fuCgl+syJjjn55iMUDRRiCd/B+9TwUda3l9WXH5i7RU53QGRCsDVFZxixLOlmXr9E3VSqkf8xXBnHs/5E2z5PIOCN0nxfB9xeA1db/QxPwK4gkHisep+eNHRn9x+DpCYDoSoYQN0nBg+H3uqfOqL42mJ+tqSfkyqbhjBf1kjtDTlBfVCWtI0siu7owm+c65+8KNyPlj5/0AyJ4Aqk1OX2jv+YE4nTipucn7rHwWuowasPU86l+uBsLNwOSb+H7loJvQyhEINX2FS1KnpRU+ld20t07n+N3ErfX5xBAGfxXpoN9BKKSP+RT7rvTeXTVE ****  test@.com **** 

5. Create a Project

On the GitLab homepage, click the Create a project option:

Image 5

Click Create blank project, set the Project name and Project URL, then click Create project:

Image 6

This article uses the mywork project as an example.

6. Add SSH Key

On the current project page, click Add SSH key:

Image 7

Paste the content from your id_rsa.pub public key file into the Key text box:

Image 8

Click Add key. After the SSH key is added, it will look like this:

Image 9

Copy the Clone link, which will be needed during cloning operations:

Image 10

Using GitLab

1. Configure user information for the Git repository, including username and email.

git config --global user.name "testname"
git config --global user.email "abc@example.com"

2. Clone the created project to your local machine.

git clone git@101.132.XX.XX:root/mywork.git

Image 11

3. Upload files to the GitLab server:

Navigate to the project directory:

cd mywork/

Create the target file to upload to GitLab:

echo "test" > /home/test/test.sh

Copy the target file or directory into the project directory:

cp /home/test/test.sh ./

Add the test.sh file to the index:

git add test.sh

Commit test.sh to the local repository:

git commit -m "test.sh"

Push the file to the GitLab server:

git push -u origin main

Image 12

View the uploaded test.sh file in the web interface to confirm it has been synced to the GitLab server:

Image 13

← Env ClasspathGit Tag β†’