migrate

Migrate from TFVC to Git

If you are reading this, you are close to migrate from TFS TFVC to Git. Moving to Git is rather good decision, so congratulations! But calm your horses… How will you do this? What are your needs?

*Many people refer to TFVC (source code version system in TFS) as TFS (Team Foundation Server – system to manage tasks, builds, CI/CD, tests, source code). Try to keep in mind that TFVC and TFS are different tools.

No history

If you do not need to take history of source code with you, just copy-paste source code from TFVC to Git. That’s all. Keep old TFVC repository in read-only mode just for case when you need to track history.

History, small repository

If you need to have history migrated and your repository is small (<1GB), just give a try to TFS built-in mechanism. TFS can migrate TFVC repository to Git by itself, together with history. Details can be found here.

History, big repository

Here we come to most common enterprise scenario. Big, massive repository with multiple branches and checkins. The tool to use is called git-tfs. It is two-way bridge between TFVC and Git. I used that tool to migrate 5 big TFVC repositories till now and everyone was happy! So, let’s get cracking!

Install Git and git-tfs

Both packages are available as Chocolatey packages. If you do not have Chocolatey, install it. It takes about few seconds!

choco install git
choco install gittfs

Examine TFVC repository

Run below command to examine what root branches do you have. If your code is placed in folder instead of branches, you need to convert folder to branch. Instead of http://tfs:8080/tfs/DefaultCollection just place url to your TFS collection.

git tfs list-remote-branches http://tfs:8080/tfs/DefaultCollection

You should see output similar to this:

TFS branches that could be cloned :

 $/project/trunk [*]
 |
 +- $/project/branch1
 |
 +- $/project/branch2
 |
 +- $/project/branch3
	|
	+- $/project/branch3-1
	
Cloning root branches (marked by [*]) is recommended!

As stated above, you should clone only root branches, denoted as [*].

Go to directory where your brand new Git repository should be placed. Then fire this command:

MSYS_NO_PATHCONV=1 git tfs clone http://tfs:8080/tfs/DefaultCollection $/project/trunk . --branches=none

Migrate from TFS TFVC to Git!

Do not create new branches when migrate from TFS TFVC to Git. It can mess the history and branching points.

Init branches you want to migrate or all at once (–all).

git tfs branch --init --all

Now, let’s migration begin.

git tfs fetch --all

Above command may take even few hours. When it finishes, run it again, until you are sure that all was migrated. Some leftovers may still exist in TFVC because:

  1. TFVC likes to cheat us and report that all is fetched, but it is not (!!!)
  2. Someone else has commited to TFVC in the meantime

If there is nothing more to migrate, just add remote to shared Git repository (I assume new GitRepository located on the same TFS Collection, replace with your uri).

git remote add origin http://tfs:8080/tfs/DefaultCollection/_git/GitRepository

Now create branches from TFVC’s remotes and push them to new Git repository.

git checkout master
git rebase remotes/tfs/default master
git branch -r | xargs -I BR git rebase remotes/tfs/BR BR
git push origin *:*

Instead of

git branch -r | xargs -I BR git rebase remotes/tfs/BR BR

you can copy branches you need, filtering them with regex like in command below:

git branch --list 'your-regex' | xargs -I BR git rebase remotes/tfs/BR BR

Congratulations! You have just migrated from TFS TFVC to Git repository with whole history. It is still reasonable to leave TFVC repository in read-only mode as backup.

About the author

Programista lubiący ten fach. Połączenie perfekcjonisty i skauta z odrobiną eksperymentatora. Pracował dla dużych i małych, polskich i zagranicznych, prywatnych i publicznych podmiotów. Miał również przyjemność opiekować się praktykantami w jednej z poprzednich firm, jak również prowadzić ćwiczenia na Politechnice Warszawskiej, której jest dumnym absolwentem.

Comments

  1. Pingback: dotnetomaniak.pl
  2. Nice art!
    But I’d want to see more “why” in first paragraph. Especially if Git is significantly better solution than TFS then why is so?
    So my idea for your next article – “is decentralized solution always better than centralized?” 🙂

    1. Maciej, thank you for your message and idea. I am quite Git enthusiast, but I will do my best to present both styles (centalized and decentralized) in one of the coming posts. By the way: there are some scenarios which Git can not handle, but TFVC can! Stay tuned!

Leave a Reply

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