Reusable Git Deployment Setup Guide

This guide outlines configuring a remote deployment setup using Git over SSH with a bare repository and post-receive hook. Ideal for streamlined deployments with minimal operational overhead.

1. Remote Server Setup

1.1 Create Bare Git Repository

mkdir -p /home/site/dev
cd /home/site/dev
git init --bare
    

1.2 Create Deployment Directory

mkdir -p /home/site/public_html
chown -R youruser:www-data /home/site/public_html
    

1.3 Add a Post-Receive Hook

cd /home/site/dev/hooks
nano post-receive

Save to post-receive:

#!/bin/bash
GIT_WORK_TREE=/home/site/public_html git checkout -f

Add permission with shell command:

chmod +x post-receive

2. Local Workstation Setup (Windows)

2.1 Generate SSH Key

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

Save it to C:\Users\YourName\.ssh\id_rsa

cat ~/.ssh/id_rsa.pub | ssh youruser@yourserver "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
    

2.2 Configure SSH for Custom Port

Edit C:\Users\YourName\.ssh\config:

Host yourproject
    HostName yourdomain.com
    User youruser
    Port 2222
    IdentityFile C:\Users\YourName\.ssh\id_rsa
    

3. Local Git Repository

3.1 Initialize and Push Code

git init
git remote add origin yourproject:/home/site/dev
git add .
git commit -m "Initial commit"
git push origin master
    

To clone your repo later:

git clone ssh://user@domain:port/home/site/dev
    

4. Optional: Auto-Deployment Script

Create a file named deploy.bat:

@echo off
echo Deploying to yourproject...
git push origin master
pause
    

Notes