Introduction
Having control over your development process is crucial for any project. One way to achieve this is to set up your own private Git server. This enables you to manage your repositories without relying on third-party services like GitHub or GitLab. Follow this step-by-step guide to setting up a private Git server on an Ubuntu system, equipped with multiple code examples from basic to advanced.
Prerequisites
Before you begin, ensure that you have:
- An Ubuntu server (18.04 or later is preferred)
- Root or sudo privileges on the server
- Basic understanding of command-line operations and Git
Step-by-Step Instructions
Step 1: Installing Git
sudo apt update
sudo apt install git
After installing, you can verify the installation with:
git --version
Step 2: Creating a Git User
sudo adduser git
Remember the password you set for the git user as it will be used later for authentication.
Step 3: Creating a Git Repository
sudo -u git -H mkdir -p /home/git/repositories/my_project.git
cd /home/git/repositories/my_project.git
sudo -u git -H git init --bare
The –bare flag creates a repository without a working directory.
Step 4: Adjusting Permissions
sudo chown -R git:git /home/git/repositories
sudo chmod -R 770 /home/git/repositories
This sets the ownership to the git user and restricts access to them.
Step 5: Setting Up SSH Access
SSH keys are required for secure communication with the server. On your local machine run:
ssh-keygen
Upload your public key to the server:
ssh-copy-id git@your-server-ip
On the server, as the root or with sudo, edit the ssh configuration:
nano /etc/ssh/sshd_config
Ensure that the following lines are set:
PubkeyAuthentication yes
PasswordAuthentication no
After making the changes, restart the ssh service:
sudo systemctl restart ssh
Step 6: Cloning and Using Your New Repository
On your local machine:
git clone git@your-server-ip:/home/git/repositories/my_project.git
Now you can use the repository as you would with any Git service.
Step 7: Advanced Configurations
Setting Up Git Hooks
Git hooks can automate tasks upon certain triggers. Inside the bare repository:
cd /home/git/repositories/my_project.git/hooks/
nano post-receive
Add actions you want to automate within the script and make it executable:
chmod +x post-receive
Creating a Backup Solution
Regularly back up your git repositories. One simple approach:
sudo -u git -H tar czf my_project_backup-$(date +%Y-%m-%d).tar.gz /home/git/repositories/my_project.git
Store backups in a secure location.
GitWeb for Browsing Repositories
Install GitWeb for a web interface:
sudo apt install gitweb
Update its configuration pointing /etc/gitweb.conf to the repositories directory:
nano /etc/gitweb.conf
$projectroot = "/home/git/repositories";
Ensure your web server e.g., Apache, is configured to serve GitWeb.
Conclusion
By following these steps, you have taken control of your own Git server. This configuration offers full administrative powers, increased privacy, and insight into your development process. With your server running, you and your team can collaborate on projects while keeping your repositories off public platforms.