Git分支能否与master分支同服务器共存?生产测试分支关联问询
Great question! This is a super common workflow for separating production and test environments with Git, and it’s totally achievable. Let’s break down two straightforward ways to set this up, depending on your preference:
Option 1: Separate Git Clones (Simple & Beginner-Friendly)
This is the easiest approach if you’re still getting comfortable with Git. You’ll maintain two separate copies of your repository—one linked to your production directory (master branch) and another linked to your test directory (your specific branch):
Set up the production environment:
- Navigate to your web app deployment directory on the server:
cd /path/to/production/web/app - Clone your Git repository here, then confirm you’re on the
masterbranch (it’s the default for most repos, but double-check):git clone https://your-repo-url.git . git checkout master - Whenever you need to update production with the latest master changes, just pull:
git pull origin master
- Navigate to your web app deployment directory on the server:
Set up the test environment:
- Go to your test directory:
cd /path/to/test/web/app - Clone the same repository, then switch to your specific test branch (let’s say it’s called
develop):git clone https://your-repo-url.git . git checkout develop - To refresh the test environment with the latest changes from your test branch:
git pull origin develop
- Go to your test directory:
Pro tip: If you want to automate updates (like after merging a PR to master), you can set up simple shell scripts or Git hooks to trigger pulls automatically—but start with manual updates first to get the hang of the workflow.
Option 2: Git Worktree (More Efficient, No Duplicate Repos)
If you don’t want two full copies of your repo taking up server space, Git’s worktree feature lets you check out multiple branches from a single repository into different directories. Here’s how:
- First, set up your main repository (we’ll use the production directory as the base, but you can pick any location):
cd /path/to/production/web/app git clone https://your-repo-url.git . git checkout master - Add a worktree for your test branch, pointing directly to your test directory:
This will automatically checkout thegit worktree add /path/to/test/web/app developdevelopbranch into the test directory—no extra clone needed. - To update either environment:
- For production (master):
cd /path/to/production/web/app git pull origin master - For test (develop):
cd /path/to/test/web/app git pull origin develop
- For production (master):
- If you ever need to remove the test worktree later:
git worktree remove /path/to/test/web/app
Key Best Practices to Follow
- Never commit directly to
master—always make changes in your test branch, validate them thoroughly in the test environment, then merge to master via a pull request (if your repo uses that workflow). - Before pulling updates to production, take a quick backup of the current production files—just a safety net in case something goes wrong.
- Use SSH keys for your server to access the Git repo, so you don’t have to enter passwords every time you pull updates.
内容的提问来源于stack exchange,提问作者Some Dude From the Internet

