Welcome

You have reached the blog of Keith Elder. Thank you for visiting! Feel free to click the twitter icon to the right and follow me on twitter.

Migrating Code From TFS to GitHub

Posted by Keith Elder | Posted in .Net, Git, TFS | Posted on 24-07-2013

Tags: , , ,

5

Before I get started I wanted to start with a disclaimer that this article is not an article about how to use git nor is it going to discuss the differences between how TFS and Git vary when it comes to source control. I’m going to assume you’ve made the decision to either try Git out for source control or you are ready to make the jump from TFS completely to using Git for source control. Trust me this really isn’t a painful process and I’m going to make it as simple as I can. Here we go.

Installing The Tools You’ll Need

Since we are migrating code from TFS to GitHub I am going to assume you already have TFS set up and installed. I’m also assuming you are using Windows and are a developer on the Windows platform. Here are two ways to get going with git. The first way is the easiest and the second way is the *geekiest* for more advanced users.

  1. Github for Windows  – A complete install to use git on windows as well as GitHub (note does not work directly with GitHub Enterprise at this time but it doesn’t matter it still makes setting up git super simple).
  2. Chocolatey  – A Machine Package Manager, somewhat like apt-get, but built with Windows in mind. This will allow one to easily install numerous packages and if you haven’t started using it yet you’ve been missing out on awesomeness.

Setting up Git Using GitHub For Windows

This is the easiest way to really set up git source control on Windows locally (unless you are already using chocolatey). Using this gives you all the tools you’ll need to start working with git and you do not have to have admin rights on your machine, everything is installed locally. Go to http://windows.github.com/ and kick off the GitHub for Windows install. Note that you do not have to create an account on GitHub to get the tools installed locally. GitHub For Windows comes with PoshGit (a nice powershell utility) and I personally find using Powershell with git from the command line the easiest way to proceed.

After installing GitHub for Windows you’ll be presented with this screen.

image

Login if you have a github.com account or go create one (again not completely necessary).

If you don’t want to create an GitHub account just hit cancel. After doing so click tools in the top center of the screen and select options.

image

In the default shell section select the shell you wish to be opened when you open the “Git Shell” from the install menu. For me personally I selected PowerShell since it comes set up with PoshGit and is faster than using a bash prompt on Windows. Plus PowerShell is just cool!

image

 

Once the option you want to use is selected press the Windows key on your keyboard and type “git shell”. This will search for the git shell so you can launch it. In Windows 8 it will look like this but you can do the same thing in Windows 7 or Windows Vista.

image

Launching this will launch the desired shell. You are now set up and ready to configure git (more on that later).

 

Setting up Git Using Chocolatey

 

Chocolatey is awesome and allows Windows users to easily install tons of open source software the same way Linux distributions have been doing for years. Once Chocolatey is installed it becomes trivial to install git as well as git for Team Foundation Server (TFS). To get set up with Chocolatey head over to http://chocolatey.org and follow the simple instructions. To install it all you are going to do is paste a line into a command window.

Once it is installed you can then easily install git and git tfs with the two following commands:

cinst git

cinst gittfs

These installs will look like this from a PowerShell window:

image

image

Configuring Git

Either way you decided to go on the setup above (GitHub Windows or Chocolatey) you now have git and git tfs installed. Now it is time to configure git. Git needs to know two things before we get started and those are username and email. Note I have re-printed the following information from https://help.github.com/articles/set-up-git.

Username

First you need to tell git your name, so that it can properly label the commits you make.

git config –global user.name "Your Name Here"

# Sets the default name for git to use when you commit

Email

Git saves your email address into the commits you make. We use the email address to associate your commits with your GitHub account.

git config –global user.email "your_email@example.com"

# Sets the default email for git to use when you commit

Note: if you are using github.com you’ll need to use the email address associated with your account, if you are using an internal github enterprise server you’ll need to do the same

Migrating Code From TFS

Now that git is set up and configured locally we need to pull our code down from TFS locally. The purpose of the *git tfs* tool is it allows us to pull our code from TFS while also saving the commit history. If you’ve read up on git you know it is distributed which means everyone has a full copy of the repository locally.

I’ve set up a sample folder in TFS called XAmple. It only has one file in it that has been modified and checked back into TFS. It is this code base that we want to move to git.

image

To move this we are going to open the Git Shell. If you installed GitHub for Windows press the windows key and type “git shell” and click the icon. If you used Chocolatey open up PowerShell. Once you are at the command prompt we are going to use git tfs to pull all the code and changes from TFS down and have a git repository created for us locally.

The command to run is:

git tfs clone http://{nameofserver}:8080/tfs/SoftwareCollection $/_your_path

Here is the example from the TFS example above:

image

Note that we had 3 commits in TFS and we also have 3 commits showing up in our PowerShell window. Note for larger projects this can take some time to pull everything down.

Typing “git log” we can even see these commits directly from git.

image

Getting Our Code Into GitHub

Now that we’ve pulled our code down locally we can easily push it up into GitHub.Com or a GitHub Enterprise server. Here are the steps needed to get this going.

Before being able to push code to GitHub be sure you’ve set up an SSH key in GitHub and that it works. In other words you can authenticate and clone another repository.

The first thing we are going to do is create a repository in GitHub.

image

On the next screen fill in the fields. We’re going to assume this is a public repo and we’re going to add a README as well as set up the .gitignore file for CSharp (this is so files that don’t need to be committed don’t get put into the repo by accident).

image

We are then going to select the SSH option and copy the location in the SSH form. We’re going to then add a remote location to the repository that was created using git tfs.

image

 

Next step is to add this remote location to our local git repository so we can push and pull code to GitHub or GitHub Enterprise. I’m using our internal Git server so your settings may vary.

image

Next we are going to pull down the repository we created so the README and .gitignore get updated locally.

image

And then we are going to then push everything backup.

image

Now that everything is pushed up to GitHub anyone else can fork our repo, issue pull requests and we can even see the history of our files in GitHub.

image

And that is it! We’ve just taken a TFS repository, pulled it down as a git repository, and pushed it back up into GitHub. Once the tooling is installed this process goes pretty quickly. The thing that takes the longest is to pull the changes from the TFS server down locally, but once that is done it is just a few commands to push things back up into GitHub.

That’s it. Happy gitting!