Deploying Your Website with Git
Git deployment provides a modern, reliable way to push code changes from your local development environment directly to your web hosting server. Instead of manually uploading files via FTP, you can use Git to deploy updates with a single command. This guide covers the different methods for deploying your website using Git on your {{COMPANY_NAME}} hosting account.
Why Use Git for Deployment?
Git-based deployment offers significant advantages over traditional FTP:
- Version control: Every change is tracked and can be rolled back
- Atomic deployments: All files update together, preventing partial updates
- Speed: Only changed files are transferred, not the entire site
- Automation: Deploy with a single
git pushcommand - Collaboration: Multiple developers can work on the same project safely
- History: Full audit trail of who changed what and when
Tip: All {{COMPANY_NAME}} shared hosting plans include SSH access and Git support. You can start using Git deployment immediately.
Prerequisites
Before setting up Git deployment:
- Git installed locally on your development machine
- SSH access to your hosting account (enabled by default on our plans)
- SSH key authentication configured (recommended over password authentication)
- Basic Git knowledge (commits, branches, push/pull)
Method 1: Git Push to a Bare Repository
This is the most common and reliable method for shared hosting deployment.
Step 1: Set Up SSH Key Authentication
- Generate an SSH key pair on your local machine (if you do not have one):
ssh-keygen -t ed25519 -C "[email protected]"- Copy the public key to your hosting account:
- In cPanel: Go to Security > SSH Access > Manage SSH Keys > Import Key
- In DirectAdmin: Go to Account Manager > SSH Keys
- Paste your public key content and authorize it
Step 2: Create a Bare Repository on the Server
SSH into your hosting account and create a bare Git repository:
ssh [email protected]
mkdir -p ~/repos/mysite.git
cd ~/repos/mysite.git
git init --bareStep 3: Create a Post-Receive Hook
This hook automatically copies files to your web directory after each push:
cat > ~/repos/mysite.git/hooks/post-receive << 'HOOK'
#!/bin/bash
GIT_WORK_TREE=$HOME/public_html git checkout -f main
HOOK
chmod +x ~/repos/mysite.git/hooks/post-receiveReplace public_html with your actual web root if different, and main with your branch name.
Step 4: Add the Remote to Your Local Repository
On your local machine:
cd /path/to/your/project
git remote add production ssh://[email protected]/home/user/repos/mysite.gitStep 5: Deploy
Push your code to deploy:
git push production mainYour website files will be automatically checked out to the web root.
Method 2: Using cPanel Git Version Control
cPanel includes a built-in Git deployment feature:
- Log in to cPanel
- Go to Files > Git Version Control
- Click Create to set up a new repository
- Enter the repository details:
- Clone URL: Your remote Git repository URL (e.g., GitHub, GitLab, Bitbucket)
- Repository Path: Where to store the repo on the server (e.g., /home/user/repositories/mysite)
- Repository Name: A descriptive name
- Click Create
Pulling Updates
- Go to Git Version Control in cPanel
- Click Manage next to your repository
- Click Pull or Deploy tab
- Click Update from Remote to pull the latest changes
- Click Deploy HEAD Commit to deploy the changes
Setting Up a .cpanel.yml Deploy File
Create a .cpanel.yml file in your repository root to define deployment tasks:
---
deployment:
tasks:
- export DEPLOYPATH=/home/user/public_html/
- /bin/cp -R * $DEPLOYPATHThis file tells cPanel how to deploy your files.
Method 3: GitHub/GitLab Webhooks
Automate deployment when you push to GitHub or GitLab:
Step 1: Create a Deployment Script
Create a PHP file on your server (e.g., deploy.php):
<?php
$secret = 'your-webhook-secret';
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE_256'] ?? '';
$payload = file_get_contents('php://input');
if (!hash_equals('sha256=' . hash_hmac('sha256', $payload, $secret), $signature)) {
http_response_code(403);
die('Unauthorized');
}
$output = shell_exec('cd /home/user/repos/mysite && git pull origin main 2>&1');
file_put_contents('/home/user/logs/deploy.log', date('Y-m-d H:i:s') . "\n" . $output . "\n", FILE_APPEND);
echo "Deployed successfully";Step 2: Set Up the Webhook
- Go to your GitHub repository > Settings > Webhooks
- Add a new webhook with:
- Payload URL: https://example.com/deploy.php
- Content type: application/json
- Secret: Your secret key (matching the script)
- Events: Select "Just the push event"
- Save the webhook
Now, every push to your GitHub repository will trigger a deployment.
Handling Build Steps
If your project requires a build step (e.g., npm install, composer install), add it to your post-receive hook:
#!/bin/bash
GIT_WORK_TREE=$HOME/public_html git checkout -f main
cd $HOME/public_html
# Install PHP dependencies
if [ -f composer.json ]; then
composer install --no-dev --optimize-autoloader
fi
# Install Node.js dependencies and build
if [ -f package.json ]; then
npm install --production
npm run build
fiBest Practices
- Use .gitignore properly: Exclude sensitive files (.env, config files with passwords), node_modules, vendor directories, and compiled assets from version control
- Never commit secrets: Keep API keys, database passwords, and other secrets out of Git. Use environment variables or .env files (excluded from Git)
- Test before deploying: Always test changes locally before pushing to production
- Use branches: Deploy from a specific branch (e.g., main or production) and do development on feature branches
- Keep the repository clean: Avoid committing large binary files (images, videos, databases)
- Monitor deployments: Check your site after each deployment to catch issues early
- Document your setup: Keep notes on your deployment configuration for team members
Troubleshooting
Permission Denied When Pushing
- Verify your SSH key is added and authorized in your hosting account
- Check the repository path in your remote URL is correct
- Ensure the repository directory permissions allow your user to write
Files Not Updating After Push
- Check the post-receive hook has execute permission (
chmod +x) - Verify the GIT_WORK_TREE path matches your actual web root
- Check the branch name matches (main vs. master)
- Review the hook output for errors
Merge Conflicts on the Server
- Avoid making direct changes to files on the server
- If conflicts occur, SSH into the server and resolve them manually
- Consider resetting the server copy:
git checkout -f main
Related Articles
- Setting Up Git Version Control for Your Website
- How to Access Your Account via SSH
- How to Generate and Use SSH Keys
Need help setting up Git deployment? Contact our support team at {{SUPPORT_EMAIL}} or visit {{SUPPORT_URL}}.