๐ ๏ธ Switched Branches? Clean Before You Debug
Switching Git branches in Visual Studio and immediately hitting F5 sometimes throws “Unable to start program… The system cannot find the file specified” — even though the code builds fine. The cause is almost always stale build output, not a broken project.
๐ Why This Happens
Git only tracks source files, not the bin/ and obj/ folders (they're correctly gitignored). When you switch branches, if the target framework, output path, or project references differ even slightly between branches, Visual Studio's incremental build can end up pointing at an exe/dll path that no longer matches what's actually configured - or a half-updated obj/ cache confuses MSBuild about what needs rebuilding.
โ The Fix, in Order of Effort
- Try the cheapest fix first: Build โ Clean Solution, then Build โ Rebuild Solution.
- Still broken? Close Visual Studio and manually delete the bin/ and obj/ folders in the project directory, then reopen and rebuild.
- Still broken after that? Check whether the branches actually target different .NET versions/SDKs — that requires more than a clean, it needs the right SDK installed.
๐งน Manual Clean (PowerShell, run from solution root)
Get-ChildItem .\ -Include bin,obj -Recurse | Remove-Item -Recurse -Force # Then in Visual Studio: # Build -> Rebuild Solution # Debug -> Start Debugging (F5)
โ ๏ธ Make This Automatic Going Forward
- Visual Studio has no built-in “auto-clean on branch switch” — but you can add a simple pre-build check or just make Clean-before-Rebuild a habit after any branch switch that touches project files (.csproj).
- If you use Git hooks, a post-checkout hook that deletes bin/obj can automate this project-wide.
“Unable to start program” right after a branch switch is Visual Studio pointing at a file path that no longer matches reality — Clean + Rebuild fixes that mismatch faster than any deeper troubleshooting.
