How To: Safely Rename a Source-Controlled File in Visual Studio .NET

Last Friday, I posted href="">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.

color=#000080 size=2>To rename a source-controlled file in Visual Studio .NET
2002 or 2003 in a way that retains file history

style="MARGIN: 3pt 0in 3pt 0.25in; mso-list: l52 level1 lfo78"> face=Verdana size=2>1. style="FONT: 7pt 'Times New Roman'">      face=Verdana size=2>Inform all project enlistees that you plan to rename a file
in the project.

class=commentbody>2. style="FONT: 7pt 'Times New Roman'">     
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

class=commentbody>3. style="FONT: 7pt 'Times New Roman'">     
the file from the project (if C++ project or there is no “Exclude” command, see
Note #2 below).

class=commentbody>4. style="FONT: 7pt 'Times New Roman'">     
Rename the
file in the SCC database using your source control provider’s standalone
application (for example, Visual SourceSafe

class=commentbody>5. style="FONT: 7pt 'Times New Roman'">     
Get Latest
Version of the file in the source control

class=commentbody>6. style="FONT: 7pt 'Times New Roman'">     
In Visual
Studio .NET, select the project and click Show All

class=commentbody>7. style="FONT: 7pt 'Times New Roman'">     
the newly renamed file in the project.

class=commentbody>8. style="FONT: 7pt 'Times New Roman'">     
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


Notes about this procedure:

  1. Can only be used to rename files, not
  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

style="COLOR: navy; FONT-FAMILY: Arial">Il class=stilemessaggiodipostaelettronica18> style="COLOR: navy; FONT-FAMILY: Arial; mso-ansi-language: EN-AU">
face=Verdana>presente posting viene fornito “così come é”, senza garanzie, e non
conferisce alcun diritto.

Comments (16)

  1. AsbjornM says:

    Nah, no blog.. yet..

  2. Nick says:

    No need to exclude the file from the Vs.NET project, you can simply
    1- rename the file in VS.NET
    2-Rename the file in VSS, with the new name you gave in VS.NET
    3- Check-in the file in VS.NET

  3. Scott Munro says:

    Surrogate primary keys for SourceSafe?

  4. Corey Brown says:

    Funny, the MS-SCCI supports the rename functionality directly, but it appears that MS has not enabled the functionality in VS.NET. I’ve been playing with our own SCCI implementation against VS.NET and just cannot get it to work. Maybe i’ll just have to tell our own customers to user your suggestion.

    Thanks for the info.


  5. Thank you for this great post about <a href=""”>" title="cyprus in property sale">cyprus in property sale</a> and [URL=]cyprus in property sale[/URL]