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

Git worktree对比Git分支:搭建内部应用开发环境是否值得采用?

Using Git Worktree for Isolated Development & Deployment on the Same Server

Great call on picking Git worktree for this use case—it’s exactly the low-overhead solution you need to keep your development and master branches separate, eliminating the risk of accidental merges or deployments from the wrong branch. Let’s break down how to set this up smoothly:

Step 1: Set Up Your Base Repository

First, clone your GitHub repo to your internal server (skip this if you already have the repo there). We’ll use a base directory for your development work, and a separate worktree for the deployment-ready master branch.

git clone git@github.com:your-org/your-internal-app.git ~/my-app-dev
cd ~/my-app-dev

Step 2: Create a Worktree for the Master (Deployment) Branch

Now, add a worktree that links directly to the master branch—this will be your deployment directory. It shares the Git object database with your base repo, so no extra disk bloat from a full clone:

git worktree add ~/my-app-deployment master

This creates a new directory ~/my-app-deployment that’s checked out to the master branch, fully isolated from your development work.

Step 3: Switch Your Base Repo to Development

Back in your base repo, switch to the development branch for your daily coding work:

git checkout development

Now you have two separate directories:

  • ~/my-app-dev: Your development space, working on the development branch
  • ~/my-app-deployment: Your deployment space, running the master branch

Step 4: Daily Workflow Tips

  • Update Deployment: When you’re ready to deploy the latest master changes, just pull in the worktree directory:
    cd ~/my-app-deployment
    git pull origin master
    
  • Development Work: Code, commit, and push to development directly from the base repo as you normally would:
    cd ~/my-app-dev
    git add .
    git commit -m "Add user dashboard feature"
    git push origin development
    
  • Manage Worktrees: To see all active worktrees linked to your base repo, run:
    git worktree list
    
  • Remove a Worktree: If you ever need to tear down the deployment worktree, use:
    git worktree remove ~/my-app-deployment
    

Why This Works for Your Scenario

The key win here is branch isolation with minimal overhead. Unlike maintaining two separate clones, Git worktree shares the core Git data, so you save disk space while keeping your development and deployment environments completely separate. No more accidentally checking out master in your dev folder and pushing untested code—each environment stays locked to its intended branch.


内容的提问来源于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.20 07:17:42