You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git分支能否与master分支同服务器共存?生产测试分支关联问询

Absolutely, You Can Do This—Here's How!

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:

    1. Navigate to your web app deployment directory on the server:
      cd /path/to/production/web/app
      
    2. Clone your Git repository here, then confirm you’re on the master branch (it’s the default for most repos, but double-check):
      git clone https://your-repo-url.git .
      git checkout master
      
    3. Whenever you need to update production with the latest master changes, just pull:
      git pull origin master
      
  • Set up the test environment:

    1. Go to your test directory:
      cd /path/to/test/web/app
      
    2. 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
      
    3. To refresh the test environment with the latest changes from your test branch:
      git pull origin develop
      

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:

  1. 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
    
  2. Add a worktree for your test branch, pointing directly to your test directory:
    git worktree add /path/to/test/web/app develop
    
    This will automatically checkout the develop branch into the test directory—no extra clone needed.
  3. 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
      
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:35:26