Last Friday, I posted a procedure for renaming source-controlled items in VS.NET that fails to mention one big gotcha: If you rename a file using Visual Studio .NET's Rename command, you lose its history--or at least the continuity of such--for the file in your source control database*. Why? Because VS.NET renames a file by essentially copying it on disk and deleting the original. If the renamed item is under source control, SCCI asks you to check out the project file, marks the newly-created (renamed) file, as a "pending add" to the source control database, and when you check in the project, adds the new file to the database alongside the old, unrenamed file; a master copy of which remains in the source control database. Unlike what happens in the filesystem, the old, unrenamed version of the file is not deleted from the SCC database. The problem with this is that while you might expect the source control history of the file to 'follow' it to the renamed version, it remains with the old, unrenamed database version of the file instead.

Many thanks to reader AsbjornM (no blog?) for calling out the existence of an alternative procedure for renaming/moving files in a way that retains history for the altered file.

Disclaimer: When developing source-controlled projects in Visual Studio .NET, Microsoft recommends that you perform all source control operations in the IDE.  Generally, you should not use your standalone source control application to directly manage a VS.NET project file in the database.

When RENAMING a file in VisualStudio.NET however, you can (and perhaps should) forget this disclaimer.  So let's try this again.

To rename a source-controlled file in Visual Studio .NET 2002 or 2003 in a way that retains file history

1.      Inform all project enlistees that you plan to rename a file in the project.

2.      Check in the file to be modified and, if your database permits multiple checkouts, ask all project enlistees to check in the entire project to which the file or project belongs

3.      Exclude the file from the project (if C++ project or there is no "Exclude" command, see Note #2 below).

4.      Rename the file in the SCC database using your source control provider’s standalone application (for example, Visual SourceSafe Explorer).

5.      Get Latest Version of the file in the source control database.

6.      In Visual Studio .NET, select the project and click Show All Files.

7.      Include the newly renamed file in the project.

8.      Delete the old, un-renamed version of the file from disk, if desired, and notify your teammates that they should do so as well. (If Web project, see note #3 below.)


Notes about this procedure:

  1. Can only be used to rename files, not projects.
  2. C++ projects and other reference-based projects types do not support the Exclude from Project/Include in Project commands because they are not 'directory-aware' like VB and C# projects. To rename files in a source-controlled C++ project, Remove the file from the project, rename the file in the source control database, and then re-add the file to the project using the Add Existing Item command.
  3. Web projects are pure, directory-based projects. All files that reside in the project folder on disk appear in the project in VS.NET. If you work with other developers, when renaming files in a source-controlled Web project, you should try to inform your teammates of the change as soon as possible to avoid confusion.

Il presente posting viene fornito “così come é”, senza garanzie, e non conferisce alcun diritto.