如何将Visual Studio 2015解决方案的Git仓库移出根目录
Got it, let's walk through this step by step—moving your Git repo files, linking your solution to the new location, and making sure Visual Studio plays nice with the change.
Step 1: Move the Git Repository Files First
First, close Visual Studio entirely to avoid any file lock issues. Then:
- Find the
.gitfolder in your solution directory (D:\works\CobraSin). This folder contains all your Git repo data (commits, branches, etc.—don't rename or modify it, just move it). - Cut this folder and paste it into your target backup directory:
C:\MainDocs\[your-specific-subfolder](replace with your actual path, likeC:\MainDocs\CobraSinGitBackup).
Step 2: Link Your Solution to the New Git Repository
Now you need to tell Git that your solution files (in D:\works\CobraSin) are connected to the repo you just moved. Open Command Prompt or Git Bash and run these commands:
Navigate to your new repo directory:
cd C:\MainDocs\[your-specific-subfolder]Configure the repo to point to your solution's working directory:
git config core.worktree "D:\works\CobraSin"Use the full absolute path here—this tells Git where to find the actual files it's tracking.
If you ever need to run Git commands directly from the solution folder without switching directories, you can use the
--git-dirflag:cd D:\works\CobraSin git --git-dir="C:\MainDocs\[your-specific-subfolder]\.git" statusBut setting
core.worktreeis the better long-term fix, as it lets Git automatically recognize the link without extra flags.
Step 3: Get Visual Studio to Recognize the New Repo
Visual Studio should pick up the Git connection automatically once the repo is configured correctly, but if it doesn't:
- Open your solution in Visual Studio.
- Go to Git > Manage Branches or open the Git Changes window. If it doesn't show your repo, head to Git > Settings (this is solution-specific, not the global Tools > Options menu).
- In the Git Settings window, under Repository Settings, check if the repository path points to your new
.gitfolder. If not, click Add and browse toC:\MainDocs\[your-specific-subfolder]\.git.
Step 4: Verify Everything Works
To make sure the setup is solid:
- Check the Git Changes window in Visual Studio—it should display any uncommitted changes like it did before.
- Run a quick test command from your solution directory:
This should return the normal repo status, confirming the link between your solution and the new repo location is working.git status
What About Visual Studio's Tools > Options Menu?
The Tools > Options > Source Control > Git Global Settings page is for global configurations (like your name, email, default merge tool)—it doesn't let you set per-solution repository paths. For that, you'll use the Git commands above and the solution-specific Git > Settings in the IDE.
Quick Troubleshooting
- If Visual Studio still doesn't see the repo, close and reopen the solution (or restart Visual Studio entirely).
- Double-check the
core.worktreesetting by runninggit config --listfrom the new repo directory—you should see the path to your solution folder listed. - Make sure the entire
.gitfolder was moved (no leftover files in the original solution directory).
内容的提问来源于stack exchange,提问作者Ricardo de Oliveira

