Getting started with Git for the TFS dev (and quick reference)

This post is aimed at those totally inexperienced with Git and what it is. So it is very basic, but also covers some useful tips and tricks I’ve picked up along the way.

Git(Hub) vs TFS (pre 2013)

The significant difference between Git and TFS to begin with is that Git is a version control system while TFS is mainly a central code repository system with some version abilities. Then there is GitHub which is a code repository running on Git. The following will not cover GitHub.

The tools

Since I want to lower the bar for trying all this out I will not point you to a bunch of websites and tell you the order to install things. Just use Chocolatey! If you haven’t used it before, just run the command on the frontpage and you are set.
After that you need Git, so run: cinst git
And then, you probably want Git-TF or Git-TFS: cinst Git-TF or cinst gittfs, or both if you want to give both a shot.
To make command line life easier with tab-completion support, and visualise your work a bit better I suggest PoshGit: cinst poshgit
If you are a heavy mouse user and not comfy with the command line you probably want to get GitExtensions, which is a graphical tool to help you with most (if not all) commands. I prefer this when heading down a nasty merge. cinst gitextensions
And last but not least, for those of you quick with the mouse there is a Visual Studio plugin to handle alot of tasks.

Working order

Using Git locally gives you some nice opportunities. Firing up a project and get started is very easy and it can help keep you sane when a large chunk of work is ahead which you do not want to check-in to TFS, yet regularly want checkpoints. Jimmy Bogard has a nice post on how to perform day to day operations. Follow these simple steps and for the most part you will be fine.

The big difference

What you must know when working with Git, is that a folder, any folder, can be a repository. This also means that when you switch branch, the current working folder will change its contents. This is important, because what happens when you have a file open and switch branch? Not to worry, nothing gets lost.

The other difference is that you do not explicitly check out anything. If it is on your machine, you have it accessible and it is never write-locked. Just code away.

The commands, quick reference

  • git init, initialize a new git repository.
  • git checkout myFeatureBranch, switch branch.
  • git checkout -b myFeatureBranch, creates a new feature branch.
  • git add -A, stage all files you want to commit, modified, new files and deleted files alike.
  • git add ., stage all modified and new files, no deleted files.
  • git add -u, stage all modified and deleted files, no new files.
  • git commit -m "Your descriptive commit message", commits all staged files with given message.
  • git rebase branchName, applies and fast forwards work from “branchName” on top of your current branch. Sort of a merge in TFS.
  • git checkout -- <filename>, if, god forbid, you made something wrong, reverts to last HEAD (commit).
  • git reset --hard HEAD, reverts entire branch to last commit undoing anything not commited.
  • git tf clone http://tfs-server:8080/tfs/DefaultCollection $/Path_In_SourceControl, initializes a new Git repo from an existing TFS path. The –deep flag is optional and clones each TFS changeset and vice versa.
  • git tf configure --deep http://tfs-server:8080/tfs/DefaultCollection $/Path_In_SourceControl, configures an existing Git repo to communicate with a path in TFS. The –deep flag is optional and clones each TFS changeset and vice versa.
  • git tf pull, fetch everything from TFS you don’t currently have on your local git repo.
  • git tf checkin, checks all your commits in to TFS. If you’ve used the deep option, each commit will be a TFS checkin as well.
  • git tfs quick-clone http://tfs:8080/tfs/DefaultCollection $/some_project, performs a quick clone letting you keep working from latest changeset.
  • git tfs fetch, fetches latest work from TFS to your local repo.
  • git tfs checkin -m "Did stuff", checkin all your work to TFS with a comment.
  • git tfs checkintool, checkin all your work to TFS using a graphical tool and builds a checkin comment consisting of all your git commits.
  • And then…

    And then you get started! Pull it down, play around with, get a feel for what you like. I really like the commandline for just staging and commiting, as well as performing basic merging where everything goes smoothly, and also pushing commits to TFS. It takes less of my focus away from what I’m currently doing and I don’t have to let go of the keyboard. But then if there’s a hairy merge coming up or a rebase gone wrong, to me the overview and messages are clearer in GitExtensions.

    Speaking of merging, as a last note I want to point out that Git performs mergings much smoother than TFS and can handle 3-way merges. So alot of pain is taken away directly. Then GitExtensions uses KDiff by default to handle the merges (still 3-way) which visualizes it all very well. I can’t remember a single merge having gone totally bonkers with this approach. And if you still don’t like git, consider setting KDiff as your standard TFS merging tool, it will help a lot.

    Further reading

Tagged ,