Using Git with Tfs

If you’ve been curious about Git but have to use TFS, there is a way to try Git locally while still using TFS on the server. This can give you a chance to learn about Git while still working with Tfs.

Install git ( and git-tfs ( Ensure both are working properly on the command line by trying out the commands.
Setup username and email address in .gitconfig. On a Windows system, it should be in your C:\Users\{username}. See a sample .gitconfig below.

quick-clone allows you to clone a TFS repository at a specific changeset (c12345 in the example below):
git tfs quick-clone http://tfs:8080/tfs/DefaultCollection “$/yourTfsBranch” -c12345 –workspace=C:/ws

Now, you can pull all of the history starting from c12345 above:
git tfs pull

Run the clean up command:
git gc
git tfs cleanup

You’re ready to use Git as your local source control.

When you have a new task, create a new branch:
git checkout -b yourNewBranchName
After you made your changes, add it to your local branch:
git add –patch

Before moving your code, you need to get the latest from TFS and merge it into your branch:
git checkout master
git tfs pull
git checkout yourNewBranchName
git rebase master

Or, you can replace all of the 4 commands above with:
git tfs pull -r

If you need to shelve your changes:
git tfs shelve ShelfsetName

To checkin your code to tfs:
git tfs checkintool

When you need to merge changes from TFS, run
git mergetool

After you are done merging, continue with git rebase
git rebase –continue

Visual Studio also provides options to manage your branches and checkin your code to your local branches without the need to use a command line. You would still use command line to move your code to TFS.

SourceTree is another excellent tool to manage your local Git.

Sample .gitconfig (using Visual Studio as a merge tool):

name = FirstName LastName
email = [email protected]
autocrlf = true
excludesfile = C:\\.gitignore
editor = ‘C:\\Program Files\\Sublime Text 3\\sublime_text.exe’ -w
fscache = true
preloadindex = true

tool = vs
prompt = false
[difftool “vs”]
cmd = \”C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\vsdiffmerge.exe\” \”$LOCAL\” \”$REMOTE\” //t
keepbackup = false
trustexitcode = true
tool = vs
prompt = false
[mergetool “vs”]
#cmd = ‘C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\vsdiffmerge.exe’ “$REMOTE” “$LOCAL” “$BASE” “$MERGED” //m
cmd = \”C:\\Program Files (x86)//Microsoft Visual Studio 12.0\\Common7\\IDE\\vsdiffmerge.exe\” \”$REMOTE\” \”$LOCAL\” \”$BASE\” \”$MERGED\” //m
keepbackup = true
trustexitcode = true


Sample .gitIgnore:
#ignore thumbnails created by windows
#Ignore files build by Visual Studio

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.