SFTP vs GitHub: Best Way to Deploy Code to a VPS
In this article
For deploying code from a local machine to a VPS, SFTP is the best choice for quick, one-off file transfers, while GitHub (with a pull-based deployment) is the best choice for ongoing, repeatable, and team-based deployments. The right answer depends on your workflow: if you are updating a single file or a small site occasionally, SFTP is simpler; if you are managing a project with multiple files, versions, or collaborators, GitHub-based deployment is more reliable and auditable.
Background: What SFTP and GitHub Actually Do
SFTP (SSH File Transfer Protocol) is a network protocol that lets you securely transfer files between your computer and a remote server over an encrypted SSH connection. It is essentially a secure version of FTP, and it is built into most SSH clients. You use an SFTP client (like FileZilla, WinSCP, or the command-line sftp tool) to browse the server's filesystem, upload, download, and manage files directly.
GitHub is a web-based hosting service for Git repositories. Git is a version control system that tracks changes to files over time, allowing multiple people to collaborate, branch, merge, and roll back changes. GitHub itself is not a deployment tool—it is a central place to store and manage your Git repositories. However, it can be used as the backbone of a deployment workflow: you push your code to GitHub, and then your server pulls the latest changes from GitHub.
The key difference is that SFTP is a direct file transfer, while GitHub-based deployment is a version-controlled, automated pipeline. SFTP is like copying a file to a USB drive; GitHub is like using a shared document with a full edit history.
How SFTP Deployment Works
With SFTP, you connect to your VPS using an SFTP client, navigate to the web root directory (e.g., /var/www/html), and upload your files. The process is straightforward:
- Open your SFTP client and enter your server's IP address, SSH port (usually 22), username, and password or SSH key.
- Navigate to the local directory where your code lives.
- Navigate to the remote directory where the website is served from.
- Drag and drop files or folders to upload them.
- Overwrite existing files as needed.
This is ideal for small sites, static HTML pages, or quick fixes. You have full control over exactly which files go where, and there is no need to set up any additional software on the server. However, it becomes tedious and error-prone when you have dozens of files or need to update the site frequently, because you have to remember which files changed and manually upload them.
How GitHub-Based Deployment Works
GitHub-based deployment typically uses a "pull" model: your server has a copy of the repository, and when you push changes to GitHub, the server pulls those changes. There are two common approaches:
- Manual pull: You SSH into your server, navigate to the repo directory, and run
git pull origin main. This requires you to log in each time, but it is simple and gives you control. - Automated via webhook: You set up a webhook on GitHub that sends a POST request to a script on your server (e.g., a PHP or Node.js script) whenever you push. The script runs
git pullautomatically. This is more convenient but requires initial setup and security considerations (e.g., verifying the request is from GitHub).
The pull model ensures that the server always has the exact same files as your repository, with no risk of missing a file or uploading to the wrong directory. It also gives you a complete history of what was deployed and when, which is invaluable for debugging.
How to Choose: Factors to Consider
Deciding between SFTP and GitHub-based deployment depends on several factors:
- Project size and complexity: If you have a single HTML file or a small PHP script, SFTP is fine. If you have a full application with hundreds of files, Git is better.
- Frequency of updates: If you update your site daily, Git saves time. If you update it once a month, SFTP is fine.
- Team collaboration: If multiple developers work on the code, Git is essential for merging changes and avoiding overwrites. SFTP has no conflict resolution.
- Server environment: If your server is a shared host with no Git installed, SFTP may be your only option. Most VPSs allow you to install Git easily.
- Rollback needs: With Git, you can revert to a previous commit in seconds. With SFTP, you need to keep manual backups of old files.
Common Problems and Pitfalls
Both methods have their own set of common issues:
- SFTP: File permissions can get messed up if you upload as a different user. Also, if you upload a file while the server is reading it, you might get a corrupted file. Always upload to a temp location and then move it, or use a tool like
rsyncwhich is more robust. - Git-based: Merge conflicts can occur if you forget to pull before editing. Also, if your server has local changes (e.g., a config file that is not in the repo), a
git pullmight fail. Use a.gitignorefile to exclude sensitive or environment-specific files. - Security: With SFTP, you are exposing an SSH port, which is fine if you use key-based auth. With Git, you need to ensure your repository is private if it contains sensitive data, and you must protect your SSH keys.
Costs and Setup Time
Both methods are free in terms of software. SFTP requires no setup beyond having an SSH server running (which is standard on most VPSs). Git-based deployment requires installing Git on the server (apt install git on Debian/Ubuntu) and setting up a repository. The main cost is time: setting up a webhook or a script to auto-pull takes about 30 minutes the first time, but it saves time on every subsequent deployment.
If you are using GitHub, the free tier is sufficient for public repositories, and private repositories are also free for small teams. However, if you are deploying from a private repo, you will need to set up an SSH key or a personal access token on the server, which is a one-time task.
Alternatives to SFTP and GitHub
There are other deployment methods worth mentioning:
- rsync: A command-line tool that syncs files between directories, only transferring changed parts. It is faster than SFTP for large directories and can be automated with cron.
- GitHub Actions or other CI/CD: You can set up a workflow that automatically builds and deploys your code to the server via SSH after every push. This is more powerful but requires learning YAML and CI/CD concepts.
- Docker: If you containerize your app, you can deploy the container to the server. This is a bigger paradigm shift but offers consistency across environments.
- Netlify/Vercel: For static sites, these platforms offer drag-and-drop or Git-based deployment without needing a VPS at all.
Frequently Asked Questions
Q: Can I use both SFTP and Git together?
Yes. Many developers use Git for version control and SFTP to upload the built files to the server. This is common when the server does not have Git or when you only want to upload the compiled output.
Q: Is SFTP secure enough for production?
Yes, SFTP is secure because it uses SSH encryption. The main risk is if you use weak passwords or expose SSH to the internet. Use SSH keys and disable password authentication for better security.
Q: How do I handle database changes with Git?
Git tracks code, not database data. You should either use migrations (scripts that alter the database schema) or manually export/import the database. Tools like Laravel's migrations or Django's migrations work well with Git-based deployments.
Q: What if I accidentally upload a file with the wrong permissions?
You can fix permissions via SFTP by right-clicking and setting permissions, or via SSH with chmod. For Git, you can set the file mode in the repository, but it is often easier to set permissions in a post-deploy script.
Q: Do I need to use GitHub specifically, or can I use GitLab/Bitbucket?
Any Git hosting service works the same way. The process is identical: push to the remote, then pull on the server. The choice is personal preference.
Q: How do I automate deployment without a webhook?
You can use a cron job on the server that runs git pull every few minutes. This is less efficient than a webhook but works for low-traffic sites.
In summary, start with SFTP for simplicity, but switch to Git-based deployment as soon as your project grows or you need to collaborate. The initial setup effort pays off in reliability and peace of mind.