There are two types of projects maintained in TFS (Microsoft).
- TFVC (Scrum)
- Agile
What if you want to move your Source Control Repository and the content to GIT?
This functionality is provided in TFS where a person selects the Repository type and then Repository name. Then, you have to select the Repository you want to move and click on the "OK" button to migrate all the content and the branches along with the history from Source Control (TFVC) to GIT.
But what if a user wants to move it through the code itself? Yes, there is a way in .NET where you can migrate your repository; not only migrate but you can create your own repository.
In NuGet Packages, you can find a NuGet related to Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.SourceControl.WebApi.
You have to install the packages as well as all the dependent packages. The following code will help you in creating and migrating the repository.
- VssCredentials creds = new VssClientCredentials();
- creds.Storage = new VssClientCredentialStorage();
- VssConnection connection = new VssConnection(new Uri(c_collectionUri), creds);
- GitHttpClient gitClient = connection.GetClient < GitHttpClient > ();
- //The below code will Create New Repository and returns you the respository's ID
- Guid targetProjectGuid = new Guid(cbp._Key);
- TeamProjectReference teamProjectReference = new TeamProjectReference();
- teamProjectReference.Id = targetProjectGuid;
- GitRepositoryCreateOptions gitRepositoryCreateOptions = new GitRepositoryCreateOptions();
- gitRepositoryCreateOptions.Name = textBox2.Text; //New Repository Name
- gitRepositoryCreateOptions.ProjectReference = teamProjectReference;
- var obj = gitClient.CreateRepositoryAsync(gitRepositoryCreateOptions).Result; //Create new repository in target Project
- Guid targetRepositoryGuid = obj.Id;
- GitImportRequest gitImportRequest = new GitImportRequest();
- GitRepository gitRepository = new GitRepository();
- gitRepository.Id = targetRepositoryGuid;
- gitImportRequest.Repository = gitRepository;
- GitImportTfvcSource gitImportTfvcSource = new GitImportTfvcSource();
- gitImportTfvcSource.Path = "$/" + cbpSource._Value.ToString();
- gitImportTfvcSource.ImportHistory = false;
- gitImportTfvcSource.ImportHistoryDurationInDays = 30;
- GitImportRequestParameters gitImportRequestParameters = new GitImportRequestParameters();
- gitImportRequestParameters.TfvcSource = gitImportTfvcSource;
- gitImportRequestParameters.GitSource = null;
- gitImportRequest.Parameters = gitImportRequestParameters;
- var resultBack = gitClient.CreateImportRequestAsync(gitImportRequest, targetProjectGuid, targetRepositoryGuid).Result;
The above "resultBack" will be the response after the migration of the repository and its content.
References Involved
- using Microsoft.TeamFoundation.Client;
- using Microsoft.TeamFoundation.Core.WebApi;
- using Microsoft.TeamFoundation.SourceControl.WebApi;
- using Microsoft.TeamFoundation.WorkItemTracking.Client;
- using Microsoft.VisualStudio.Services.Client;
- using Microsoft.VisualStudio.Services.Common;
- using Microsoft.VisualStudio.Services.WebApi;

Join the conversation! Your thoughts help the community grow.