Windows生产脚本纳入Git后,如何切换分支不改动工作目录文件?
Great question—this is a common pain point when using Git with production scripts where you can't risk overwriting live files. Let's break down why your current approach causes issues, then walk through the best solutions to keep your production environment stable while enabling branch-based development.
Why Your Current Setup Causes Problems
Right now, you're using your live production directory as Git's working directory. When you switch branches (e.g., git checkout NewFeature), Git replaces the files in that directory with the version from the target branch. That's exactly why your development changes immediately affect the scripts being called in production—you're editing the exact same files that are in use.
Solution 1: Use Git Worktrees (Recommended)
Git 2.5+ introduced git worktree, which lets you create multiple independent working directories tied to the same Git repository. This is perfect for your use case: you can keep your production directory on the stable main branch, and have a separate working directory for your NewFeature branch (or any other development branch) that doesn't interfere with live files.
Step-by-Step Setup:
- Ensure your production repo is initialized (you've already done this part):
cd C:\Scripts\MyScript1 git init git add . git commit -m "baseline prod script" git branch NewFeature - Create a separate working directory for your development branch:
# This creates a new folder (../MyScript1_NewFeature) linked to the NewFeature branch git worktree add ../MyScript1_NewFeature NewFeature - Develop safely in the new directory:
All changes you make inC:\Scripts\MyScript1_NewFeaturewill only affect theNewFeaturebranch—your production directory (C:\Scripts\MyScript1) remains on themainbranch with unmodified live scripts. - Merge changes to production when ready:
When your feature is tested and ready, switch back to the production repo and merge the branch:cd C:\Scripts\MyScript1 git merge NewFeature - Clean up unused worktrees (if needed):
git worktree remove ../MyScript1_NewFeature
Solution 2: Use Separate Git Repositories (For Beginners)
If git worktree feels too complex, you can use two separate repositories: one for production (stable code) and one for development.
Step-by-Step Setup:
- Keep your production repo as-is (on the
mainbranch, no branch switching here):cd C:\Scripts\MyScript1 git init git add . git commit -m "baseline prod script" - Create a copy of the repo for development:
# Copy the production repo to a new development directory xcopy C:\Scripts\MyScript1 C:\Scripts\MyScript1_Dev /E /I cd C:\Scripts\MyScript1_Dev git checkout NewFeature - Develop in the dev repo:
Modify scripts inC:\Scripts\MyScript1_Devwithout touching production files. - Bring changes to production:
When ready, generate a patch from your dev branch and apply it to the production repo:# In the dev repo, create a patch file cd C:\Scripts\MyScript1_Dev git format-patch main..NewFeature -o ../patches # In the production repo, apply the patch cd C:\Scripts\MyScript1 git am ../patches/*.patch
Solution 3: Use Symbolic Links (For Unchangeable Production Call Paths)
If you can't modify the paths used to call your scripts (e.g., powershell C:\Scripts\MyScript1\MyScript1.ps1 must stay the same), you can use Windows symbolic links to point the production path to the correct branch directory.
Step-by-Step Setup:
- Create separate directories for each branch:
# Initialize main branch repo (stable production code) mkdir C:\Scripts\MyScript1_Main cd C:\Scripts\MyScript1_Main git init git add . git commit -m "baseline prod script" # Create NewFeature branch worktree (or use a separate repo) git worktree add ../MyScript1_NewFeature NewFeature - Create a symbolic link for the production call path:
# Link C:\Scripts\MyScript1 to the main branch directory mklink /D C:\Scripts\MyScript1 C:\Scripts\MyScript1_Main - Switch to development when needed:
When you want to test changes without affecting production, update the symbolic link:# Delete the old symlink rmdir C:\Scripts\MyScript1 # Link to the development branch directory mklink /D C:\Scripts\MyScript1 C:\Scripts\MyScript1_NewFeature - Switch back to production after merging:
Once you mergeNewFeatureintomain, update the symlink again to point toMyScript1_Main.
Critical Notes for Windows Scripts
- Avoid line ending issues: Windows scripts (Batch, VBScript) rely on CRLF line endings. Configure Git to not convert line endings:
git config core.autocrlf false - Lock down production repos: Set your production repo to disallow direct pushes to the
mainbranch, and require pull requests/merge reviews before changes are applied. - Backup regularly: Always keep backups of your production scripts, even with Git version control.
内容的提问来源于stack exchange,提问作者kaaron

