如何将本地更改同步到SECOND Git存储库? [英] How to sync local changes to a SECOND Git repository?
问题描述
我正在为他拥有源代码的客户项目使用本地TFS 2018实例.我希望他在无法使用时无法访问此代码.
I'm using a local TFS 2018 instance for a customer project in which he owns the source code. I want him to have complete access to this code in the event I become unexpectedly unavailable.
我的建议是创建一个TFS发布步骤,以将我的TFS origin
与他的VSTS帐户中的存储库同步.
My proposal for this is to create a TFS release step that syncs my TFS origin
with a repo in his VSTS account.
我已经能够使用此方法从我的工作站手动完成此操作:
I've been able to do this manually, from my workstation, using this approach:
cd /path/to/local/repo
git remote add tfs url://tfs/git/repo
git push --mirror tfs
但是提示我输入他的VSTS凭据,这显然在TFS发布步骤中不起作用.
But I'm prompted for his VSTS credentials, which obviously won't work in a TFS release step.
此外,尝试从TFS服务器的桌面remote add
时遇到此错误:
Also, I encounter this error when attempting to remote add
from the desktop of my TFS server:
D:\Agent\_work\37\s>git remote add Application https://customer.visualstudio.com/Applications/_git/Application
Rename from 'D:/Agent/_work/37/s/.git/config.lock' to 'D:/Agent/_work/37/s/.git/config' failed. Should I try again? (y/n)
我可以在工作站上添加它,尽管它不会显示为可以提交给TFS的push
的更改.因此,看来我无法将远程VSTS信息库告诉TFS.
I can add it at my workstation, although it doesn't show up as a change that can be committed for a push
to TFS. So it seems I'm unable to tell TFS about the remote VSTS repository.
如何在TFS发布步骤中完全自动化此任务?
How can I fully automate this task in a TFS release step?
推荐答案
我可以结合使用以下答案来实现此目的:
I was able to get this working using a combination of these answers:
- https://stackoverflow.com/a/45224858/722393
- https://stackoverflow.com/a/50925741/722393
诀窍是使用VSTS用户配置文件的安全性"部分中的个人访问令牌(PAT)".
The trick is to use the Personal Access Token (PAT), found in the security section of the VSTS user profile.
这是我最后得到的脚本:
Here're the scripts I ended up with:
SyncCode.ps1
[CmdletBinding()]
param(
[Parameter(Mandatory)][string] $Application,
[Parameter(Mandatory)][string] $LocalPath,
[Parameter(Mandatory)][string] $Token
)
. \\SERVER\Scripts\TfsBuild\Invoke-Git.ps1
$RemoteExists = $false
$AllRemotes = git remote
ForEach($Remote in $AllRemotes) {
If($Remote = $Application) {
$RemoteExists = $true
Break
}
}
If($RemoteExists) {
Invoke-Git -Command "remote remove $Application"
}
Invoke-Git -Command "remote add $Application https://Personal%20Access%20Token:$Token@customer.visualstudio.com/Applications/_git/$Application"
Set-Location $LocalPath
Invoke-Git -Command "push --mirror HydraMonitor"
从SyncCode.ps1调用的Invoke-Git.ps1
<#
.Synopsis
Invoke git, handling its quirky stderr that isn't error
.Outputs
Git messages, and lastly the exit code
.Example
Invoke-Git push
.Example
Invoke-Git "add ."
#>
Function Invoke-Git
{
param(
[Parameter(Mandatory)]
[string] $Command )
Write-Output "##[command]. git $Command"
Try {
$Exit = 0
$Path = [System.IO.Path]::GetTempFileName()
Invoke-Expression "git $Command 2> $Path"
$Exit = $LASTEXITCODE
If ( $Exit -gt 0 ) {
Write-Error (Get-Content $Path).ToString()
}
Else {
Get-Content $Path
}
"Exit code: $Exit"
}
Catch {
Write-Host "Error: $_`n$($_.ScriptStackTrace)"
}
Finally {
If ( Test-Path $Path ) {
Remove-Item $Path
}
}
}
这篇关于如何将本地更改同步到SECOND Git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!