Migrate an existing project from TFS to Git with changeset history intact
When I first 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 in order to pull out the changeset history and then commit that to a new git repo. At worst, I would have to say goodbye to a almost two years of change history and start all over. To my surprise, I faced neither scenario thanks to an amazing open source tool called Git-TF! Git-TF does all the heavy lifting for you 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. Best way on Windows is via Chocolatey since it will automatically wire up the PATH for you.
choco install git-tf -y
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 length of your changeset history or 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 some others to make things a bit more git friendly.
- Remove the TFS source control bindings from solution. Now, you could have done this from within Visual Studio, but its just as easy to do it manually. Simply remove all of the
*.vsssccfiles and make small edit to your .sln file removing the
GlobalSection(TeamFoundationVersionControl) ... EndGlobalSectionin your favorite text editor.
- Add a
.gitignorefile. It's likely your Visual Studio project or solution will 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 into your new repo.
- Add a
.gitattributesfile to help git understand how to deal with visual studio specific files. e.g. How should a .csproj file be treated? Here is a good 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 for you 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. Note though that GitHub is just my example here, the remote could easily be Bitbucket, Stash, or all three.
That's it! Project published with all history intact.