Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloning all remote repositories from TFS

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.

like image 441
Harsha W Avatar asked Aug 29 '19 04:08

Harsha W


People also ask

How do I clone all repositories of an organization?

(1) Right click inside the folder you want all of the repos to clone to (2) select "Git Bash Here" - Bash window will open (3) Edit the script with your org name, then copy and paste it into the bash window (maybe make sure line endings are unix based, not sure, I did just to be safe). (4) it should start running.

How do I clone a TFS repository?

In Team Explorer, select Connect to open the Connect page, and then choose Clone under Local Git Repositories. Enter the clone URL of the remote Git repo that you want to clone, verify the local folder path where you want to create the local clone, and then choose Clone.

Can we use git to clone from TFS?

Cloning the whole TFS Project CollectionYou can clone all projects by specifying $/ as the tfs-repository path. If you do not specify a git repository name, it will clone into tfs-collection .

How do I clone a remote repository?

To clone a Git repository, you will first copy the remote URL from your repository hosting service—in this case GitHub. You will then use the Git clone command followed by the remote repo's URL. If you are working with a private repository, you will be prompted for your remote hosting service credentials.


2 Answers

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

    })
})
like image 137
Shayki Abramczyk Avatar answered Sep 18 '22 21:09

Shayki Abramczyk


I did not see that feature directly in TFS, but if the VSTS API is also available for your on-premise TFS instance, you can:

  • use the API to get all projects, and then all repositories for a given account
  • use that list to loop and pull each master branch in a local clone of those repositories, that you can then archive.
like image 33
VonC Avatar answered Sep 19 '22 21:09

VonC