Subversion with Visual Studio
This page describes the basic techniques for using the Subversion source control system with Visual Studio.
It is assumed that you already have a Subversion repository that you wish to use. This repo may or may not already have files in it. If you want information about installing the Subversion server on a Windows box, check out my Subversion Cheat Sheet article. Although these days I am partial to using online Subversion hosts like Project Locker.
To use Subversion on your PC then you will probably want to grab both of these Subversion clients:
TortoiseSVN is an svn client that integrates into Windows Explorer. AnkhSVN is a plug-in that tightly integrates with Visual Studio. Just run the installers for both tools; they are very straightforward and will not be described here.
Before jumping into Visual Studio give Tortoise a try. Bring up Windows Explorer then navigate to any directory. Right-click a blank area in the directory listing pane then select TortoiseSVN then Settings from the pop-up menu. There is only one Tortoise setting that you must change in order for Visual Studio and ASP.NET to work properly with Subversion. In this settings window—in the General settings section—be sure that you have Use "_svn" instead of ".svn" directories option selected.
With that done, close the Settings then bring up the Tortoise repo browser (again, right-click a blank area in any directory listing pane in Windows Explorer and use the Tortoise menu). A window titled URL will appear. Enter the URL of one of your repos in the URL field, then click the OK button. The URL may point to a remote repo (starting with http://) or to a local file system repo (starting with file://). The repo browser window will now appear and you can look at the files in your repo, show the change history of any file, and perform any other Subversion function you want with the repo.To “manually” pull down a repo you could right-click the trunk directory in the example shown in the screen-shot to the right, then select the Checkout command. This would download all of the project’s files to a local directory on your PC and you could then open them with Visual Studio. However, it is much easier to do this sort of thing directly from Visual Studio. Tortoise is handy when you need to get at the repo outside of Visual Studio.The rest of the article describes how to work with your repo from within Visual Studio and the AnkhSVN plug-in, with a diversion to the command line client at the end.Creating a Subversion Repo
I’ll assume that you already have a Visual Studio solution that you would like to upload to your svn repo, so go ahead any open your solution with Visual Studio.With the solution open, right-click the solution node in the Solution Explorer window and select Add Solution to Subversion. In the Add to Subversion window, you will probably want to keep the Project Name the same as the solution name (the field will be automatically populated for you) but go ahead and change it if you need to. Enter the address of your repo in the Repository URL field. After a few seconds the Subversion Repositories section should become populated. Highlight repo node you wish to place your repo into (make note of the Create Folder button on the right). I always use the conventional trunk/branches/tags svn subdirectories, and if you will be too then click the Add trunk Folder for Project option.Go ahead and click the OK button. Svn will upload your solution to the repo. Once the upload is complete then right-click the solution node within the Solution Explorer window again, and select Commit Solution Changes. You have just created your first Visual Studio solution repo!Opening a Subversion Repo
Bring up Visual Studio and have the URL of your repo and name of your solution handy. On the File menu, choose Subversion → Open from Subversion. In the Url field, enter the address of your repo. It may take a few seconds for svn to go out to your repo and populate the window.
Depending on your solution’s structure you may have to double-click a couple of nodes until you get to the solution of your Visual Studio solution’s .sln file. Once you get there, double-click on the .sln file name. This will cause the Open Project from Subversion window to appear.
The From drop-down should be left as it is and you’ll probably want to keep the Type field set to Latest Version and the Append Branch or Tag Name to the Local Directory option unchecked. Lastly, browse to the directory you would like to keep the solution in on your PC and then click the OK button.
The solution will be downloaded from the repo and then Visual Studio will open it. That’s it; you are now ready to add, edit, and delete files in the solution will the full power of source control to help you in your development efforts.
Source Control Operations
A source control system like svn keeps track of the files you add to, edit in, or remove from a solution. Once a solution has been placed under source control (svn in this case) you will see a new set of icons appear beside each file and directory in your Visual Studio solution.
Modifying an Existing File
To modify an existing file in a solution you simply open the file and start typing, just as you always have. The difference is that the svn status icon next to the file will automatically change to an icon that indicates the file has been edited locally and needs to be updated on the server in order for other developers to see your changes.
The biggest benefit to a source control system is that it keeps two developers from modifying the same file at the same time. Well, it doesn’t actually keep this from happening, but it will always tell you if you try to update a file in the repo that was modified by someone else since the last time you updated your copy from the repo.
Creating a New File
To create a new file in your solution you right-click the project node or appropriate subdirectory name and then choose Add Item, again as you always have. Once the file is added to the solution then the svn icon for it will be the icon that indicates that this is a new file and must be added to the repo on the server in order for others to see the file.
Pending Changes Window
With a source control system installed (AnkhSVN in this case) and with your solution under source control, you can see an overview of all the files you have changed and/or added since the last time you did a sync with the svn server by using the Pending Changes window. You can show this window by going to Visual Studio’s View menu and selecting Other Windows → Pending Changes.
You can also look at your repository outside of Visual Studio’s “view” of the repo. Visual Studio’s Solution Explorer window only shows you the repo files that have been made a part of a solution/project, but the underlying directory of a solution/project may have additional files than just those that are a part of the solution proper. You can see all of the files that are a part of your repo, as well as files that are in other repos, directly from Visual Studio by going to the View menu and selecting Solution Explorer.
The Solution Explorer window is very much like the TortoiseSVN view (see above) of a repo.
SVN From the Command Line
The article Subversion Best Practices is a great place to start reading about some essential ways to work with svn, including the concepts behind the standard trunk, branches, and tags repo directories, as well as other repo guidelines.