Overview of Git-Based Deployments
Deploying static websites or single-page applications (built with Astro, Vite, Hugo, or HTML/CSS) via FTP or web uploaders is repetitive and error-prone.
By leveraging Git alongside a bare repository and a post-receive hook, you can publish updates to your live web server with a single terminal command:
git push production main
This workflow ensures atomic file updates, minimal bandwidth consumption, and consistent file permissions.
Step 1: Configure SSH Key Authentication
Automated Git deployment requires passwordless SSH access to avoid entering credentials on every push.
- Generate an SSH keypair on your local computer if you do not already have one:
ssh-keygen -t ed25519 -C "deploy-key" - View your public key:
cat ~/.ssh/id_ed25519.pub - Connect to your web hosting account and append the public key to
~/.ssh/authorized_keys:mkdir -p ~/.ssh chmod 700 ~/.ssh echo "your-copied-public-key" >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys - Verify from your local machine that you can connect without entering a password:
ssh -p 22 cpaneluser@server-hostname
Step 2: Initialize a Bare Git Repository on the Server
Connect to your hosting server via SSH and create a directory dedicated to storing repository metadata outside your public web root.
# Navigate to your user root directory
cd ~
# Create a directory for your git repositories
mkdir -p ~/git/my-website.git
# Navigate into the folder and initialize a bare repository
cd ~/git/my-website.git
git init --bare
A bare repository contains the Git version history and branches without working files, making it safe to accept inbound pushes.
Step 3: Create the Post-Receive Deployment Hook
Git hooks are executable bash scripts triggered by specific repository events. The post-receive hook fires immediately after the server finishes receiving pushed objects.
Create the hook script inside ~/git/my-website.git/hooks/post-receive:
cat << 'EOF' > ~/git/my-website.git/hooks/post-receive
#!/bin/bash
TARGET="$HOME/public_html"
GIT_DIR="$HOME/git/my-website.git"
echo "Deploying updates to web root: $TARGET..."
# Checkout the latest main branch files into the target public web directory
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f main
# Ensure correct file and directory permissions
find $TARGET -type d -exec chmod 755 {} +
find $TARGET -type f -exec chmod 644 {} +
echo "Deployment complete! Live site updated."
EOF
Make the script executable:
chmod +x ~/git/my-website.git/hooks/post-receive
Step 4: Add the Production Remote on Your Local Computer
Now return to your local computer terminal, navigate into your local website project directory, and register your server as a Git remote:
cd /path/to/local/project
# Add the remote named 'production'
git remote add production ssh://cpaneluser@server-hostname:22/~/git/my-website.git
# Verify the remote configuration
git remote -v
Ensure your primary working branch is named main:
git branch -M main
Step 5: Test the Deployment Workflow
Stage, commit, and push your site files to your new production remote:
# Stage any pending changes
git add .
# Create a commit
git commit -m "Configure automated Git deployment"
# Push to the hosting server
git push production main
Your terminal will display the live output from the server’s post-receive hook:
Enumerating objects: 24, done.
Counting objects: 100% (24/24), done.
Writing objects: 100% (24/24), 18.42 KiB | 2.63 MiB/s, done.
Total 24 (delta 6), reused 0 (delta 0)
remote: Deploying updates to web root: /var/www/public_html...
remote: Deployment complete! Live site updated.
To ssh://server-hostname:22/~/git/my-website.git
* [new branch] main -> main
Visit your domain in a web browser to verify that the updated static files are live.
Advanced: Building on the Server (Static Site Generators)
If your website uses a static build framework like Astro or Hugo, you can compile assets directly on the server rather than pushing build directories:
- Push your source files (
package.json,src/, etc.) to an application directory (e.g.~/apps/my-site). - Update your
post-receivescript:#!/bin/bash APP_DIR="$HOME/apps/my-site" WEB_ROOT="$HOME/public_html" GIT_DIR="$HOME/git/my-website.git" git --work-tree=$APP_DIR --git-dir=$GIT_DIR checkout -f main cd $APP_DIR npm install --production=false npm run build rsync -av --delete dist/ $WEB_ROOT/
This provides a lightweight, self-hosted Continuous Deployment (CD) pipeline without third-party CI service dependencies.
Frequently Asked Questions
Why should I deploy static sites via Git instead of FTP?
Git deployments transfer only modified delta files rather than re-uploading entire directories, provide complete version rollback history, and can trigger automated server-side hooks to purge caches or set file permissions upon deployment.
What is a bare Git repository and why is it needed for deployments?
A bare Git repository contains only version control metadata and object databases without an active working directory. Pushing to a bare repository avoids conflicts with checked-out files and allows a post-receive hook to safely checkout production files into your public_html directory.
Can I deploy static generators like Astro, Next.js, or Hugo using this method?
Yes. You can either build the static dist directory locally and push the build artifacts, or push the repository source code and execute your build script (npm run build) directly inside the post-receive hook if Node.js or Hugo is installed on the server.
Need Technical Assistance?
Web Hosting Wale support is available 24/7. Tickets are typically answered within 10-15 minutes.