Git Server Setup: Creating a New User

Learn how to set up a Git server by creating a new user account. This guide covers essential steps for collaboration and remote repository management using Git.


Git - Create Operation

In this chapter, we will explore how to create a remote Git repository, commonly referred to as the Git Server. A Git server is essential for enabling team collaboration.

Create New User

To start, we need to add a new user and assign them to a group.

Syntax

# add new group
[root@CentOS ~]# groupadd dev

# add new user
[root@CentOS ~]# useradd -G devs -d /home/gituser -m -s /bin/bash gituser

# change password
[root@CentOS ~]# passwd gituser
        
Output

Changing password for user gituser.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
        

Create a Bare Repository

Now, let’s initialize a new repository using the init command with the --bare option. This will create a repository without a working directory. By convention, the bare repository is named with a .git extension.

Syntax

[gituser@CentOS ~]$ mkdir project.git
[gituser@CentOS ~]$ cd project.git/
[gituser@CentOS project.git]$ git --bare init
        
Output

Initialized empty Git repository in /home/gituser/project.git/
        

Generate Public/Private RSA Key Pair

Next, we need to configure the Git server by generating an RSA key pair using the ssh-keygen utility. This key pair will be used for user authentication.

Syntax

[tom@CentOS ~]$ ssh-keygen
        
Output

Generating public/private rsa key pair.
Enter file in which to save the key (/home/tom/.ssh/id_rsa): Press Enter Only
Your identification has been saved in /home/tom/.ssh/id_rsa.
Your public key has been saved in /home/tom/.ssh/id_rsa.pub.
        

Adding Keys to authorized_keys

For authentication, both users (Tom and Jerry) need to add their public keys to the server using the ssh-copy-id command.

Syntax

[tom@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub gituser@git.server.com
        
Output

gituser@git.server.com's password:
Now try logging into the machine, with "ssh 'gituser@git.server.com'", and check in:
.ssh/authorized_keys
        

Push Changes to the Repository

After creating a bare repository and setting up user access, Tom and Jerry can push their changes to the repository. Below is how Tom commits and pushes his changes.

Syntax

[tom@CentOS ~]$ mkdir tom_repo
[tom@CentOS ~]$ cd tom_repo/
[tom@CentOS tom_repo]$ git init
[tom@CentOS tom_repo]$ echo 'TODO: Add contents for README' > README
[tom@CentOS tom_repo]$ git add .
[tom@CentOS tom_repo]$ git commit -m 'Initial commit'
[tom@CentOS tom_repo]$ git remote add origin gituser@git.server.com:project.git
[tom@CentOS tom_repo]$ git push origin master
        
Output

Counting objects: 3, done.
Writing objects: 100% (3/3), 242 bytes, done.
To gituser@git.server.com:project.git
* [new branch] master → master
        

Now, the changes have been successfully pushed to the remote repository.