Reputation: 241
I have a question about what best to do in this situation. I want to write a script that creates a local repository and then can fetch and push from a VSTS to TFS to create a backup.
Using GIT Bash i was able to make this work following the instructions below.
git clone --mirror https://github.com/exampleuser/repository-to-mirror.git
cd repository-to-mirror.git
git remote set-url --push origin https://github.com/exampleuser/mirrored
git fetch -p origin
git push --mirror
I want to make a Powershell script with ISE on windows 10 that also does this. I installed git-posh to be able to write commands.
But when I write commands as instructed by GIT (https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/)
git clone https://github.com/username/repo.git
Username: your_username
Password: your_token
I get error:
remote: TF401019: The Git repository with name or identifier Logic-Test1.git does not exist or you do not have permissions for the operation you are attempting.
Username: : The term 'Username:' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
How to do the authentication so I can clone it? Also, is there a way of not having to type the authentication for every command?
Upvotes: 2
Views: 8665
Reputation: 31043
This blog Script to clone all Git repositories from your VSTS collection provides an example on how to clone a git repo from VSTS using powershell, you can check it and follow it for other commands.
First, create a PowerShell script file:
# Read configuration file
Get-Content "CloneAllRepos.config" | foreach-object -begin {$h=@{}} -process {
$k = [regex]::split($_,'=');
if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) {
$h.Add($k[0], $k[1])
}
}
$url = $h.Get_Item("Url")
$username = $h.Get_Item("Username")
$password = $h.Get_Item("Password")
# Retrieve list of all repositories
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$headers = @{
"Authorization" = ("Basic {0}" -f $base64AuthInfo)
"Accept" = "application/json"
}
Add-Type -AssemblyName System.Web
$gitcred = ("{0}:{1}" -f [System.Web.HttpUtility]::UrlEncode($username),$password)
$resp = Invoke-WebRequest -Headers $headers -Uri ("{0}/_apis/git/repositories?api-version=1.0" -f $url)
$json = convertFrom-JSON $resp.Content
# Clone or pull all repositories
$initpath = get-location
foreach ($entry in $json.value) {
$name = $entry.name
Write-Host $name
$url = $entry.remoteUrl -replace "://", ("://{0}@" -f $gitcred)
if(!(Test-Path -Path $name)) {
git clone $url
} else {
set-location $name
git pull
set-location $initpath
}
}
Then create a config file alongside your script with your configuration:
[General]
Url=https://myproject.visualstudio.com/defaultcollection
[email protected]
Password=YourAccessToken
Upvotes: 2