Source control is an invaluable tool in a developer’s arsenal. The benefits far outweigh the overhead of using source control. At the very least, it provides a kind of backup for your IP, should you accidentally make an unwanted change without an undo possibility or lose a file. It saves you from such catastrophes as your computer or hard-drive dying on you. It’s also a huge time saver when you suddenly discover that what used to build or what used to work just fine no longer does, and you have no idea why. (It happens more often than you think.) With source control, you can simply go back to a previous version of your source and do line-by-line comparisons (aka diffs) to what has changed, or just retrieve an older copy and incrementally re-add changes until you know what causes the break. The advantages are a lot more significant if more than just one person is working on the code.
Despite all the benefits, and due to being the only code in my code base, for the longest time I did without source control for my own apps. Since storage costs were so low, I simply made a copy of my solution folder every time I made a big change, typically at least once a day. Every few days, I would also make another copy of the last-known-good version to a different computer. (I can imagine some readers rightfully shaking their heads.) One reason I continued to do so is because I was familiar with the source control (TFS) used by Visual Studio Team Foundation edition, which wasn’t available to me outside of work, and the apparently very popular Git source control seemed too cryptic to understand in what little amount of time I had.
Well, now that I’ve come to the point where I have way too many apps to maintain, and TFS now offers a free service (up to 5 users at the time of writing) and even includes a Git compatible version, the stars are aligned to right this wrong. This lengthy introduction brings us to today’s topic:
How to migrate your existing Windows or Windows Phone app solution (or any other Visual Studio solution for that matter) to TFS + Git powered source control.
It’s actually easier to do than it feels at first. I will walk you through most of it with simple illustrated steps.
1. If you haven’t done already, create a new TFS account at http://tfs.visualstudio.com. The name of the account will be part of the URL for the web-based version of TFS for your account and will house all your code bases. Apparently you can create only one TFS account per Microsoft account (i.e. email address) so pick a name wisely. For instruction’s sake, I will assume you picked “tutorial”.
2. Go to http://tutorial.visualstudio.com
You can choose to create just a team project or a team project with Git. The official Visual Studio TFS Blog says that ultimately a preferential choice. Git does boast offline capabilities, so this tutorial will focus on the Git version.
3. Click on
4. Enter a project name, for example “BlogEntry”. Set the Version control to Git and click Create project.
After a few seconds, your project should be created.
5. Click Navigate to project.
You will now be taken to the home page for your project, which acts as a dashboard and a project management hub. I will not go into details about this but to note that you can easily access this page from your account.
6. Click CODE from the top menu.
Under the Explorer sub section, the Contents indicates that the repository is empty. This page will tell you the URL that you will need later on to connect to your Git repository.
7. Make a note of the URL after “git clone”: https://tutorial.visualstudio.com/DefaultCollection/_git/BlogEntry
Our intent is not to start a new project but to migrate an existing project. That’s why we will ignore the suggestion to “git clone” the repository. For the most part, we are now done with the web side of the tutorial.
Migrating Your Visual Studio Solution
1. At your local, make a copy of your existing project to a local path that is easily accessible. We will use C:\GIT\BlogEntry.
2. Open the Solution in your Visual Studio. In Solution Explorer, right-click on the solution (root) node, and select Add Solution to Source Control.
3. Select Git
4. Open the Team Explorer window (VIEW > Team Explorer)
If this is your first time, you may be prompted to install a Git tool called “Visual Studio Tools for Git”. You can also install it from its home at Visual Studio Gallery. Once it’s installed, it will look like this:
If this is your first time, you will also have to enter initial settings here, such as your user name and email address that you will use to access the Git repository.
5. Click Changes. All the important files in your project should have automatically been included similar to below:
Conveniently, some files and folders in your solution, such as object files, binaries, etc., will be excluded from the changelist by the rules defined in a special file called .gitignore. This file was created (alongside other Git specific files) in the root of your solution folder.
6. Enter a commit message (e.g. “My first commit”) and click commit. This operation is local and no files have actually been transferred to the server. To make sure your changes are reflected on the server side, you must “push” your commits. Click on Commits in Team Explorer.
7. In the textbox, paste the URL that you noted earlier in the tutorial. In our example, it was: https://tutorial.visualstudio.com/DefaultCollection/_git/BlogEntry. Click Publish.
If you mistyped the URL or some other error occurs, don’t worry. Even though I found that the commit has now disappeared (which seems like a bug), you can start over and try again as follows:
- Click the icon that looks like a plug.
- In the Local Git Repositories, right-click on the project and select Open in File Explorer
- Again under Local Git Repositories, click New and create a dummy entry. Select this entry to be active. This will allow us to do the next step.
- Right-click on the original project and select Remove.
- In the File Explorer that you launched in step #2, delete the hidden folder .git and the files .gitattributes and .gitignore.
- Go back to the beginning of the tutorial titled “Migrating Your Visual Studio Solution”
Once you’ve succeeded you can confirm that the files are indeed copied to the server by launching your TFS web page and looking under the Code section.
Congratulations! You’ve just migrated your project to Git powered source control hosted by Visual Studio in the cloud.
Verifying your Repository (Optional)
It makes sense to verify that the integrity of your Visual Studio solution is preserved during the migration. To do this, we will create another local repository by cloning our solution in the server.
1. Close the current solution. In the Team Explorer Window, click Clone and enter the Git Repository URL. Also specify a new location for the new repository.
After a few moments, your files should be downloaded to the folder of your choice.
2. Simply go and open the solution from that location and try to build it.
With a little bit of luck everything will work fine. If not, check the Solution Explorer for broken references or missing files. For the former, check the path. Usually this can be easily fixed by removing the reference that had a project specific path and re-adding it with a global path. For the latter, you can try checking .gitignore to see if any of the files were excluded.
Thank you for reading. If this was useful to you, please leave feedback.