Thursday, June 17, 2021

Set Up a Private Git Server

Ubuntu 20.04
GIT 2.17.1



Goals:
  • Create a private Git repository server without the restrictions of the providers free plans.
  • Replicate the state of an origin repository, including all the branches (including master) and all the tags as well.


Install:
  • SSH keys:
    • If you have a  ~/.ssh  folder but don't have a public key on it (e.g: id_rsa.pub):
      •  Generate a public key using the private key using the command below:
        • ssh-keygen -y -f  /home/ubuntu/.ssh/id_rsa  >  /home/ubuntu/.ssh/id_rsa.pub
    • Show the ssh public key. It will be used later during the installation.
        • cat /home/ubuntu/.ssh/id_rsa.pub
  • Git Server:
    • Install Git server:
      • sudo apt update
      • sudo apt install git
    • Create a git user and a base repository folder:
      • sudo useradd -r -m -U -d /home/git -s /bin/bash git
      • sudo su - git
        • mkdir ~/.ssh
        • chmod 0700 ~/.ssh
        • touch ~/.ssh/authorized_keys
        • chmod 0600 ~/.ssh/authorized_keys
        • Copy the content of the file  /home/ubuntu/.ssh/id_rsa.pub  to the file  /home/git/.ssh/authorized_keys. Also add the public keys of any users you want to access your private git server.
          • nano ~/.ssh/authorized_keys
        • cd /home/git
        • Create the git base repository name (optional).
          • mkdir /home/git/private-repo
  • Mirror an existing Git repository:
    • Go to the git base directory and clone the existing repo using https. One advantage of using https is that we don't need to have a firewall rule to allow ssh port traffic:
      • cd /home/git/private-repo
      • git clone --mirror https://github.com/velosomarcus/aws-kubectl.git
        • Inform username and password to do the mirror over https.
    • Open a terminal on another computer to test the access to the private repo. The ssh public key of the computer/user should be added to the  /home/git/.ssh/authorized_keys  of the private Git Server computer:
      • cd /home/ubuntu
      • git clone git@192.168.1.105:private-repo/aws-kubectl.git
      • Expected output:
        • Cloning into 'aws-kubectl'...
    • After that, everytime we want to update the mirror repo we need to:
      • sudo su - git
      • cd /home/git/private-repo/aws-kubectl.git
      • git remote update

Testing:
  • Create a new empty repository:
    • Open a terminal on the Git Server machine.
      • sudo su - git 
    • Create an empty repo:
      • git init --bare /home/git/private-repo/project-name.git
      • Expected output:
        • Initialized empty Git repository in /home/git/private-repo/project-name.git/
    • Configuring a local Git Repository, potentially on another machine:
      •  cd /path/to/local/project
      • git init .
    • Add the git remote to your local repository:
      • git remote add origin git@192.168.1.105:private-repo/project-name.git
    • Create a test file:
      • touch test_file
      • git add .
      • git commit -m "Initial commit"
      • git push -u origin master
      • Expected output:
        • Counting objects: 3, done.
        • Writing objects: 100% (3/3), 218 bytes | 218.00 KiB/s, done.
        • Total 3 (delta 0), reused 0 (delta 0)
        • To 192.168.1.105:private-repo/project-name.git
        •  * [new branch]      master -> master
        • Branch 'master' set up to track remote branch 'master' from 'origin'.
    • It is important to note that the remote repository must exist before you add the git remote to your local repository.
    • To be able to push the local git changes to the private Git server you’ll need to add your local user ssh public key to the remote `git` user’s  `/home/git/.ssh/authorized_keys` file. To add a new collaborator, just copy its public ssh key to the `git` authorized_keys file.

References: