Skip to main content
    Back to Blog
    Developers

    Git Deployment for Beginners: Push Code to Your Server

    Learn how to deploy code with Git instead of FTP — connect GitHub to your VPS, set up push-to-deploy, manage environment variables, and roll back mistakes.

    Chris GraboMarch 27, 20266 min read

    If you're still uploading files to your server via FTP, there's a better way. Git deployment lets you push code from your local machine to your server with a single command — and it gives you version history, rollbacks, and a professional workflow that scales with your projects.

    What Is Git Deployment?

    Git is a version control system that tracks every change you make to your code. Git deployment means using Git not just for tracking changes, but as the mechanism for getting your code onto your server. Instead of manually copying files, you push a commit and your server automatically pulls and applies the changes.

    The basic flow looks like this:

    • You write code on your local machine
    • You commit your changes with git commit
    • You push to a remote repository (GitHub, GitLab, or Bitbucket)
    • Your server detects the push and pulls the updated code

    That's it. No FTP client, no dragging files, no accidentally overwriting the wrong version.

    Why FTP Is Outdated

    FTP worked fine in 2005. In 2026, it's a liability:

    • No version history. Once you overwrite a file via FTP, the previous version is gone unless you kept a local copy.
    • No rollbacks. If a deploy breaks something, you have to manually figure out what changed and undo it file by file.
    • No collaboration. Two people editing the same file via FTP will overwrite each other's work.
    • Security concerns. Standard FTP transmits credentials and files in plain text. SFTP solves the encryption problem but not the workflow problems.
    • Error-prone. Forgetting to upload one file or uploading to the wrong directory is easy and can take your site down.

    Setting Up Git Deployment on a VPS

    If you have a VPS, you have full control over your deployment pipeline. Here's the simplest path to push-to-deploy using GitHub.

    Step 1: Set Up Your Repository

    If your project isn't already in Git, initialize it locally:

    • Run git init in your project directory
    • Create a .gitignore file to exclude files that shouldn't be deployed (node_modules, .env files, build caches)
    • Commit your code and push to GitHub

    Step 2: Connect Your Server to GitHub

    On your VPS, generate an SSH key and add it as a deploy key on your GitHub repository. This gives your server read access to pull code without storing your GitHub password.

    • Run ssh-keygen -t ed25519 on your server
    • Copy the public key to your repository's Settings > Deploy Keys
    • Clone the repository to your web root: git clone git@github.com:you/your-repo.git /var/www/site

    Step 3: Create a Deploy Script

    Write a simple shell script that your server runs on each deploy:

    • Pull the latest code with git pull origin main
    • Install dependencies (npm install, composer install, etc.)
    • Run build steps if needed (npm run build)
    • Restart your application process

    Keep this script in your repository so it's versioned along with your code.

    Step 4: Automate with Webhooks

    GitHub can send a webhook to your server every time you push to a branch. Set up a small listener on your server (a simple Node.js or PHP script) that receives the webhook and triggers your deploy script. Now pushing to main automatically deploys to production.

    For more advanced setups, GitHub Actions can run tests, build your project, and deploy — all triggered by a push.

    Environment Variables

    Never commit secrets to your repository. Database passwords, API keys, and encryption keys belong in environment variables, not in your code.

    On your server, store these in a .env file that's excluded from Git via .gitignore. Your application reads them at runtime. This way, your repository stays safe to share and your credentials never end up in version history.

    Rollbacks: Your Safety Net

    One of the biggest advantages of Git deployment is the ability to roll back instantly. If a deploy breaks something, you can revert to the previous working version with:

    • git log to find the last good commit
    • git checkout <commit-hash> to switch to it
    • Or git revert to create a new commit that undoes the problematic changes

    Compare that to FTP, where "rolling back" means hoping you have an old copy of every changed file somewhere on your laptop.

    SpectraHost's Built-In Git Deployment

    SpectraHost VPS plans support Git deployment out of the box. Connect your GitHub, GitLab, or Bitbucket repository, configure your deploy branch, and push. Your code is live in seconds, with full rollback capability.

    For developers who want infrastructure that stays out of the way, check out our developer-focused hosting features — including SSH access, cron jobs, and support for Node.js, Python, Ruby, and PHP.

    Start Deploying the Right Way

    Git deployment isn't just for large teams or complex applications. Even a solo developer with a single WordPress theme benefits from version history and one-command deploys. Once you've experienced it, FTP feels like sending files by carrier pigeon.

    Already using Git? Learn more about what a VPS gives you and why it's the natural home for Git-based workflows.

    Ready to Get Started?

    Free SSL, instant activation, and a 30-day money-back guarantee on every plan.