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: , , ,

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!

  • keithelder

    My reply is going to be much shorter than John’s and not as elegant. Git is a bolted on afterthought to TFS because TFS was losing mind share and market share to Github. I refer to it as lipstick on a pig.

  • John Fair

    You’re actually asking 2 different things. GitHub is an ALM in its own right (GitFlow: https://github.com/nvie/gitflow). If you’re interested in keeping your existing TFS (ALM) workflow and just using Git as the underlying VCS (technically DVCS) that’s one thing. However if you find the tooling around the TFS ALM to be cludgy, that’s where GitHub can come to the rescue. GitHub is built around the DVCS features of Git itself, and provides a whole series of features that many engineers (especially open source ones) find appealing. One of those features is the ability to make Pull Requests. PRs allow a dedicated Gatekeeper to maintain the integrity of the repository by reviewing all of the changes as they come in the door. It’s like a mini-code-review on each feature set.

    There are a plethora of other features worth investigating though: Hooks, code comparisons, built in Wiki pages, built in Issue Management (not project planning, not agile boards, just issues), Network history (who’s done what, when), etc.

    I would probably advise against a 1:1 comparison between TFS and GitHub on the ALM level. GitHub is a tool for engineers (and possibly QA), but isn’t for project managers or business analysts who are interested in tracking stories, tasks, progress, etc. That’s why GitHub offers dozens of hooks to popular systems like Trello, Pivotal Tracker, et al. They aren’t trying to be one tool to rule them all (which TFS is), but for the engineering workflow it’s THE tool to use (IMHO).

    Hope that helps some 🙂

  • browniepoints

    Okay now I’m really curious. Do you mean it’s a time sink because of the source control or because of the other ALM features. I’m genuinely interested to know. When I say Git for TFS, I mean the new native Git source control support in TFS not Git2TFS.

    Outside of pull requests, what are some other features GitHub has that’s missing in TFS?

  • keithelder

    TFS is a time sink in my opinion in terms of productivity. It doesn’t support pull requests and many other things that GitHub does.

  • browniepoints

    Just curious why GitHub and not Git for TFS?