Need a self-hosted Git server? Whether you're working on private projects or collaborating over a local network, setting up a Git server on an external hard drive can be a simple and efficient solution. In this guide, we'll walk you through the process of:
First, plug in your external hard drive and determine its drive letter (e.g., E:\).
Install Git for Windows and enable OpenSSH Server:
Windows Powershell Add-WindowsFeature -Name OpenSSH-Server Start-Service sshd Set-Service -Name sshd -StartupType Automatic
Create a bare Git repository on the external drive:
bash cd /mnt/e/GitRepos mkdir myproject.git cd myproject.git git init --bare
On a remote machine, add the Git server:
bash git remote add origin ssh://your-username@your-windows-ip/e/GitRepos/myproject.git git push origin main
bash git clone \\YourWindowsPC\GitRepos\myproject.git git push origin main
Create a script called backup_git.bat:
bat @echo off robocopy E:\GitRepos F:\GitBackup /E /MIR /LOG:C:\backup_logs\git_backup.log /NFL /NDL
Schedule this script to run daily using Windows Task Scheduler.
Download and install Gitea:
powershell cd C:\Gitea .\gitea.exe web
Gitea will now be available at http://localhost:3000. Set it up to manage repositories via a web interface.
Inside your repository's hooks folder, create a post-receive script:
bash #!/bin/bash echo "Push received at $(date)" >> /mnt/e/GitRepos/logs/push.log GIT_WORK_TREE=/var/www/myproject git checkout -f main
Make it executable:
bash chmod +x post-receive
Create a GitHub Actions workflow in .github/workflows/deploy.yml:
yaml name: Deploy on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Deploy via SSH run: | ssh user@your-server 'cd /var/www/myproject && git pull origin main'
Install Jenkins and create a job that pulls changes from E:\GitRepos\myproject.git every 5 minutes.
bash cd /var/www/myproject git pull origin main
By following this guide, you've successfully transformed an external hard drive into a private Git server with automation features. Now, you can push, manage, and deploy your code seamlessly!