Git worktree对比Git分支:搭建内部应用开发环境是否值得采用?
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 thedevelopmentbranch~/my-app-deployment: Your deployment space, running themasterbranch
Step 4: Daily Workflow Tips
- Update Deployment: When you’re ready to deploy the latest
masterchanges, just pull in the worktree directory:cd ~/my-app-deployment git pull origin master - Development Work: Code, commit, and push to
developmentdirectly 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

