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

Windows生产脚本纳入Git后,如何切换分支不改动工作目录文件?

How to Manage Production Windows Scripts with Git Without Disrupting Live Workflows

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.

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:

  1. 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
    
  2. 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
    
  3. Develop safely in the new directory:
    All changes you make in C:\Scripts\MyScript1_NewFeature will only affect the NewFeature branch—your production directory (C:\Scripts\MyScript1) remains on the main branch with unmodified live scripts.
  4. 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
    
  5. 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:

  1. Keep your production repo as-is (on the main branch, no branch switching here):
    cd C:\Scripts\MyScript1
    git init
    git add .
    git commit -m "baseline prod script"
    
  2. 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
    
  3. Develop in the dev repo:
    Modify scripts in C:\Scripts\MyScript1_Dev without touching production files.
  4. 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
    

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:

  1. 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
    
  2. 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
    
  3. 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
    
  4. Switch back to production after merging:
    Once you merge NewFeature into main, update the symlink again to point to MyScript1_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 main branch, 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:33:31