Migrate an existing project from TFS to Git with changeset history intact
When I started searching on the topic of moving from my 2-year-old TFS repository on visualstudio.com over to a new git repository, I was mentally preparing myself for a struggle.
When I started searching on the topic of moving from my 2-year-old TFS repository on visualstudio.com over to a new git repository, I was mentally preparing myself for a struggle. I figured, at best, that I would be able to write some code to pull out the changeset history and then commit that to a new git repo. At worst, I would have to say goodbye to almost two years of change history and start all over. To my surprise, I faced neither scenario thanks to an excellent open-source tool called Git-TF! Git-TF does all the heavy lifting by "cloning" your team project repository to a local git directory and even allowing you to commit and push back if so inclined. For my purposes here, though, I just needed the clone part of its functionality to make my migration as painless and fast as possible.
Step 1 - Install
Install Git-TF. The best way on Windows is via Chocolatey since it automatically wires up the PATH for you.
choco install git-tf -y
No Chocolatey or not on Windows? Then follow the manual instructions on CodePlex https://gittf.codeplex.com/
Step 2 - Clone
Clone your TFS or visualstudio.com team project branch from the command line to the directory of your choice.
You will then be prompted for your credentials (Alt credentials if using visualstudio.com). Once accepted, the download will begin and could take some time, depending on the length of your changeset history or the size of your repository.
Step 3 - Prep & Cleanup
Now that you have an exact replica of your team project branch as a local git repository, it's time to clean up some files and add others to make things more git friendly.
- Remove the TFS source control bindings from the solution. You could have done this from within Visual Studio, but it's just as easy to do manually. Simply remove all of the
*.vsssccfiles and make a small edit to your .sln file removing the
GlobalSection(TeamFoundationVersionControl) ... EndGlobalSectionin your favorite text editor.
- Add a
.gitignorefile. Your Visual Studio project or solution will likely have some files you won't want in your repository (packages, obj, etc) once your solution is built. A near complete way to start is by copying everything from the standard VisualStudio.gitignore file into your own. This will ensure all of the build-generated files, packages, and even your ReSharper cache folder will not be committed to your new repo.
- Add a
.gitattributesfile to help git understand how to deal with Visual Studio files. e.g. How should a .csproj file be treated? Here is an excellent example of what you should include in yours https://github.com/Danimoth/gitattributes/blob/master/CSharp.gitattributes. You'll also find a similar attributes file created if you've ever initialized a git repo using the GitHub client for Windows.
- Add an optional
README.mdfile (markdown or text). A standard GitHub convention that I've become accustomed to, even for private repositories. Great way to quickly outline things about your project that would be useful at a glance, such as build instructions or requirements.
Step 4 - Commit & Push
Now that I've git-ified the repository, it's time to commit the files, add the remote, and push the new branch out to GitHub. Although GitHub is my example, the remote could easily be Bitbucket, Stash, or all three.
For a great git tutorial series, I highly recommend Atlassian's
Or, for putting a face on the console, check out SourceTree
That's it! The project was published with all history intact.