Programming

Install and Configure Git Server on Ubuntu 20.04

In this tutorial, we will see how to install and configure git server on Ubuntu 20.04

Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Prerequisites

  • A Ubuntu 20.04 installed dedicated server or KVM VPS.
  • A root user access or normal user with administrative privileges.
  • Add DNS A record of your server’s hostname. For example we are using hub.hostnextra.com as our server hostname. Or else use your server IP address in the place of hub.hostnextra.com.

Install and Configure Git Server on Ubuntu 20.04

Let’s get started with installation. There are two ways to install Git.

Option 1 is install Git using APT

Keep the server up-to-date

# apt update -y

Install Git

# apt install git -y

Verify the installation:

# git –version

Option 2 is install git from source

You can download latest version of Git from release page. It make take longer time and will not be updated and maintained through the yum package manager. But it will allow you to download a newer version than what is available through the CentOS repositories, and will give you some control over the options that you can include.

First, install dependencies

# apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc

After the installation complete, go to release page and copy the download link. You can find tar.gz, right click on it and copy the link.

Now, download it in the server using wget command and rename it:

# https://github.com/git/git/archive/refs/tags/v2.31.1.tar.gz -O git.tar.gz

Once the download is complete, we can extract the tar file

# tar -zxf git.tar.gz

Now, go to that directory to begin configuring our build.

# cd git-*

Now, you can make the package and install it by typing these two commands:

# make prefix=/usr/local all
# make prefix=/usr/local install

Now, replace the shell process so that the version of Git we just installed will be used:

# exec bash

We have built and installed Git successfully. To verify it check the version using following command:

# git –version

Configure Git

Add user to handle the repositories:

# adduser git

Then give your Git user a password:

# passwd git

Log in as a git user

# su – git

Initiate a new empty repository using following command:

# git init –bare ~/hostnextra.git

Enable post-update hook by copying the sample file as follows:

# cd hostnextra.git/hooks/
# cp post-update.sample post-update

That’s it for server side.

Now let’s go to client side:

Install Git

# apt install git -y

Once the installation gets completed, start the configuring the Git

Configure Git

Submit inflammation about yourself so that commit messages will be generated with correct information attached:

# git config –global user.name “git”
# git config –global user.email “[email protected]

Create a directory where you can keep all your projects

# mkdir ~/dev
# cd ~/dev

Now, create a clone the hostnextra.git repository that we have created earlier in the server

# git clone [email protected]:~/hostnextra.git hostnextra.git
Cloning into ‘hostnextra.git’…

It will ask to enter git user password:

[email protected]’s password:

warning: You appear to have cloned an empty repository.

Go to respository

# cd hostnextra.git

You can see the repository is empty, so lets create some files

# echo “my test file” > file1.txt

Add these file to our git repository

# git add .

Commit the changes

# git commit -am “My First Commit”
[master (root-commit) b337197] My First Commit
1 file changed, 1 insertion(+)
create mode 100644 file1.txt

Push these changes to the remote git repository at hub.hostnextra.com

# git push origin master

you will be asked for password, enter git user password

[email protected]’s password:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 229 bytes | 76.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To hub.hostnextra.com:~/hostnextra.git
* [new branch] master -> master

Verify the changes, access the git server and run following command to check the logs

# git log

Output will be similar like:

commit b3371975bd44fb4aca344e365fa635180967f7fe (HEAD -> master)
Author: git <[email protected]>
Date: Wed Apr 14 10:06:06 2021 +0000

My First Commit

We have successfully install and configure Git server on Ubuntu 20.04.

Related Articles