Cloning all remote repositories from TFS

Issue

Is there a way to clone all the master branches from all the projects uploaded under 1 account.

I have a requirement to backup all the code in master branch every week.
Is there a way to accomplish this using git, Powershell or any other way?

Note that I required to do this task in a Windows environment.

Solution

You can do it with PowerShell and TFS Rest API.

First of all, get the projects with Projects – List API, then get to each project the repositories with Repositories – List API and clone only the master.

For example, a simple PowerShell script that do it:

$collection = "http://tfs-server:8080/tfs/collection-name"

$projectsUrl = "$collection/_apis/projects?api-version=4.0"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -UseDefaultCredentials -ContentType application/json

$projects.value.ForEach({

    $reposUrl = "$collectionurl/$($_.name)/_apis/git/repositories?api-version=4.0"
    $repos = Invoke-RestMethod -Uri $reposUrl -Method Get -UseDefaultCredentials -ContentType application/json
    $repos.value.ForEach({
    git clone $_.remoteUrl --branch master --single-branch

    })
})

Answered By – Shayki Abramczyk

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published