现有GitHub仓库通过SSH部署到Bluehost生产服务器的流程咨询
Absolutely, this setup is totally feasible—and you can absolutely keep your existing GitHub repository while adding automated deployments to Bluehost. Your current workflow with GitHub Desktop will only get more efficient, no need to ditch anything you're already using. Let’s break this down step by step, tailored to your setup.
1. Is This Feasible?
100% yes. Git supports multiple remote repositories, so you can configure your local repo to push changes to both GitHub (your existing origin remote) and Bluehost simultaneously. No more manual FTP drag-and-drop—one push does it all.
2. Will My Existing GitHub Repository Affect the Deployment?
Nope, not at all. Your GitHub repo will stay intact, with all its commit history, PRs, issues, and collaboration features unchanged. We’re just adding an extra "destination" for your pushes; GitHub remains your primary code repository, and Bluehost becomes your deployment target.
3. Step-by-Step Setup for Your Existing GitHub Repo
Let’s get this configured, starting with the Bluehost server side, then your local repo.
3.1 Prepare Bluehost for Git Deployment
First, log into your Bluehost server via SSH (use Terminal on Mac/Linux, PuTTY or Windows Terminal on Windows—you can find your SSH credentials in Bluehost’s cPanel under "SSH Access").
- Navigate to your user’s home directory (not your website’s public root, we’ll create a bare repo here):
cd ~ - Create a bare Git repository (this acts as a "hub" to receive pushes, since we don’t want a working directory in your public web folder):
mkdir your-project-name-deploy.git cd your-project-name-deploy.git git init --bare - Create a
post-receivehook—this script will automatically sync the pushed code to your website’s public root folder after you push to the bare repo:
Paste this code (replacenano hooks/post-receive/home/your-bluehost-username/public_htmlwith your actual website root path—you can find this in cPanel under "File Manager"):#!/bin/sh git --work-tree=/home/your-bluehost-username/public_html --git-dir=/home/your-bluehost-username/your-project-name-deploy.git checkout -f master - Save the file (in nano, press
Ctrl+O, thenEnter, thenCtrl+X), then give it execution permissions:chmod +x hooks/post-receive
3.2 Add Bluehost as a Remote to Your Local Repo
Open your project in GitHub Desktop, then click Repository > Open in Terminal (or just navigate to your local repo folder in Terminal manually).
- Add Bluehost as a new remote repository:
For example, if your Bluehost username isgit remote add bluehost ssh://your-bluehost-username@your-domain.com/home/your-bluehost-username/your-project-name-deploy.gitjpsweeney94and your domain isexample.com, it would look like:git remote add bluehost ssh://jpsweeney94@example.com/home/jpsweeney94/your-project-name-deploy.git - Verify the remote was added correctly:
You should see two entries:git remote -vorigin(pointing to your GitHub repo) andbluehost(pointing to your Bluehost bare repo).
3.3 Configure Automatic Pushes to Both Remotes
You’ve got two options here—pick whichever fits your workflow best:
Option 1: Push to Both Remotes With a Single Command
If you don’t mind running a terminal command occasionally (or you can even create a bash alias for it), use this:
git push origin master && git push bluehost master
This will push to GitHub first, then Bluehost.
Option 2: Make GitHub Desktop Push to Both Remotes Automatically
To make the "Push" button in GitHub Desktop send code to both GitHub and Bluehost:
# Add your GitHub repo as a push target for origin git remote set-url --add --push origin git@github.com:your-github-username/your-github-repo.git # Add Bluehost as an additional push target for origin git remote set-url --add --push origin ssh://your-bluehost-username@your-domain.com/home/your-bluehost-username/your-project-name-deploy.git
Now, whenever you click "Push" in GitHub Desktop, Git will automatically push to both remotes in the background. You’ll see GitHub’s push status in the app, but Bluehost will get the updates too.
3.4 Test the Workflow
- Make a small change to a file in your local repo, commit it in GitHub Desktop.
- Click "Push" as you normally would.
- Log into Bluehost’s File Manager or use SSH to check your
public_htmlfolder—your updated file should be there!
4. Quick Troubleshooting Tips
- SSH Connection Errors: Make sure your local SSH public key is added to Bluehost’s
authorized_keysfile. You can do this via Bluehost’s cPanel under "SSH Access" > "Manage SSH Keys". - Hook Not Syncing Files: Double-check the paths in your
post-receivescript—they need to be absolute paths (starting with/). Also confirm the script has execution permissions (chmod +x hooks/post-receive). - Wrong Branch Deployed: If you use a branch other than
master, update thecheckout -f masterline in the hook to match your branch name (e.g.,checkout -f main).
内容的提问来源于stack exchange,提问作者jpsweeney94

