Self-Hosting Git on Your Own Server
If you don’t know what Git is yet, it’s one of the most powerful version control systems available. It can manage any kind of project—programming, design, or even casual work. Git was created by Linus Torvalds in 2005 for Linux kernel development and is now the most widely adopted version control system.
While you can use services like GitHub for public or private repositories, you can also host Git on your own server or computer.
Step 1: Install Git
Depending on your operating system:
Windows: Download the .exe from git-scm.com
macOS: Download the .dmg from git-scm.com
Linux: Use your package manager:
# Ubuntu
sudo apt-get install git-core
# Fedora
sudo yum install git
# Arch Linux
sudo pacman -S gitStep 2: Create a Git Repository Script
Create a shell script (example: smjrifle-git.sh) with the following content:
#!/bin/bash
# Self-host Git repo creation script
if [ "$#" -eq 1 ]
then
sudo mkdir $1
cd $1
sudo git init --bare
cd ..
sudo chown git:git $1 -R
sudo chmod 775 -R $1
echo "Created git repo for $1"
echo "You can clone the repo using:"
echo "git clone git@smjrifle.net:$1"
echo "Or add your codes using:"
echo "git remote add origin git@smjrifle.net:$1"
else
echo "Please pass the repository name as a parameter"
fiMake sure the script has execute permission. If not, run:
sudo chmod a+x smjrifle-git.shStep 3: Run the Script
Execute the script with the repository name:
sh smjrifle-git.sh gitdirnameThis will:
- Create a bare Git repository in the specified directory.
- Set proper ownership and permissions.
- Output commands for cloning or adding the repo.
Step 4: Clone or Add Repository
The Git URL format for self-hosted repos is:
git_username@your_ip_or_domain:path_to_repo
#Example:
git clone git@smjrifle.net:gitdirnameFor more details on ad-hoc Git sharing, check this guide: Git Ad-Hoc Sharing of Repository
