How to rename Visual Studio projects and folders with Git

TIL: How to rename Visual Studio projects and folders with Git

This post is part of my Advent of Code 2022.

These days I had to rename all the projects inside a Visual Studio solution and the folders containing them. From SomeThing.Core to Something.Core. That wasn’t the exact typo. But that’s the idea. Here it’s what I learned. It wasn’t as easy as only renaming the projects in Visual Studio.

1. Rename folders and projects manually

After digging into it, I found this StackOverflow answer to rename folders and projects inside a solution. It layouts a checklist of what to do. Here I’m bringing it with some extra steps:

First, close Visual Studio.

Then, create a backup of the .sln file. Just in case.

Next, use git mv to rename the folder from SomeThing to Something. This preserves the history of the file. Also, rename the csproj files to match the new name.

Use the next bash script to rename the folders and csproj files inside the src and tests folders.

# Replace these two values, please
old=SomeThing
new=Something

# Put here all others folders...
for folder in 'Core' 'Infrastructure'
do
git mv src/$old.$folder src/$old.$folder.Temp
git mv src/$old.$folder.Temp src/$new.$folder
git mv src/$new.$folder/$old.$folder.csproj src/$new.$folder/$new.$folder.csproj

git mv tests/$old.$folder.Tests tests/$old.$folder.Tests.Temp
git mv tests/$old.$folder.Tests.Temp tests/$new.$folder.Tests
git mv src/$new.$folder/$old.$folder.csproj src/$new.$folder/$new.$folder.csproj
done

Notice that it uses a temp folder to rename the containing SomeThing folder. I found that workaround in this StackOverflow answer. Git doesn’t go along with different casings in file names.

Next, in the .sln file, edit all instances of SomeThing to be Something, using a text editor like NotePad++.

Next, restart Visual Studio, and everything will work as before, but with the project in a different directory.

Next, right-click on the solution name and run “Sync Namespaces.”

Visual Studio Sync Namespaces option
Fix all namespaces with 'Sync Namespaces' option

Next, use “Replace in Files” to clean leftovers. For example, launchSettings.json and appSettings.json files.

Visual Studio Replace In Files
Fix all other leftovers with 'Replace In Files' option

Everything should compile. Horraaay!

That was a tedious process. Especially if, like me, you have to rename all source and tests projects in a big solution.

Given the amount of votes of the StackOverflow answer I followed, more than 400, I bet somebody else thought about automating this process.

Indeed.

2. Rename folders and projects with ProjectRenamer

ProjectRenamer is a dotnet tool that does all the heavy and repetitive work for us. It renames the folders, csproj files, and project references. Also, it could create a Git commit and build the solution.

ProjectRenamer expects the Git working directory to be clean. Stash or commit all other changes before using it.

Using a Powershell prompt, from the folder containing the .sln file, run:

> renameproject.exe SomeThing.Core Something.Core --no-commit --no-review

With the --no-commit flag, ProjectRenamer will only stage the files. And, the --no-review skips the user confirmation. It’s like the -f flag of some Unix commands.

Voilà! That’s how to rename a project inside a Visual Studio solution. The painful and the quick way. Another tool that saved me like 100 hours. For more productivity tricks, check how to use Git to format commit messages and my Visual Studio setup for C#. I never did it by hand again.

Happy coding!