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

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.

Published 22 September 03 03:08 by KorbyP

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# AsbjornM said on September 23, 2003 1:45 PM:
:) Nah, no blog.. yet..
# Nick said on October 10, 2003 7:00 PM:
# Nick said on October 10, 2003 7:01 PM:
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
# Scott Munro said on March 18, 2004 12:13 PM:
Surrogate primary keys for SourceSafe?
# Corey Brown said on July 19, 2004 11:06 AM:
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.
--Corey
# jim said on April 17, 2006 10:15 PM:
wow, i'd like to solve your problem, use <a href="http://www.yaodownload.com/utilites/file-disk-management/filemonkey/">FileMonkey</a>">http://www.yaodownload.com/utilites/file-disk-management/filemonkey/">FileMonkey</a> can rename your file safely.

http://www.yaodownload.com/utilites/file-disk-management/filemonkey/
# online shopping cart said on September 3, 2006 6:49 PM:
Thank you so much for this great post  about <a href="http://eteamz.active.com/legalbusiness/files/online-shopping-cart.html"">http://eteamz.active.com/legalbusiness/files/online-shopping-cart.html" title="online shopping cart">online shopping cart</a> and [URL=http://eteamz.active.com/legalbusiness/files/online-shopping-cart.html]online shopping cart[/URL]
# cyprus in property sale said on September 4, 2006 7:26 AM:
Thank you for this great post about <a href="http://cahoot.builtfree.org/cyprus-in-property-sale.html"">http://cahoot.builtfree.org/cyprus-in-property-sale.html" title="cyprus in property sale">cyprus in property sale</a> and [URL=http://cahoot.builtfree.org/cyprus-in-property-sale.html]cyprus in property sale[/URL]
# online horse betting said on September 5, 2006 12:27 AM:
Thank you so much for this great post  about <a href="http://eteamz.active.com/yourbusiness/files/online-horse-betting.html"">http://eteamz.active.com/yourbusiness/files/online-horse-betting.html" title="online horse betting">online horse betting</a> and [URL=http://eteamz.active.com/yourbusiness/files/online-horse-betting.html]online horse betting[/URL]
# home equity loan rate said on September 6, 2006 12:49 PM:
Thanks for the great tips about <a href="http://AgentInsurance.bravehost.com/home-equity-loan-rate.html"">http://AgentInsurance.bravehost.com/home-equity-loan-rate.html" title="home equity loan rate">home equity loan rate</a> and [URL=http://AgentInsurance.bravehost.com/home-equity-loan-rate.html]home equity loan rate[/URL]
# accommodation france holiday in said on September 6, 2006 8:12 PM:
Thanks for the great tips about <a href="http://newbusinessloan.bravehost.com/accommodation-france-holiday-in.html"">http://newbusinessloan.bravehost.com/accommodation-france-holiday-in.html" title="accommodation france holiday in">accommodation france holiday in</a> and [URL=http://newbusinessloan.bravehost.com/accommodation-france-holiday-in.html]accommodation france holiday in[/URL]

Leave a Comment

(required) 
(optional)
(required) 

Search

Go

This Blog

Syndication

Page view tracker