【问题标题】:git backup a remote repository with another remotegit用另一个远程备份远程存储库
【发布时间】:2020-11-20 02:56:36
【问题描述】:

我在 github 上有一个远程存储库和另一个用于备份的远程存储库。 由于存储库非常大,我不希望每次都使用 git push --mirror(大于 20GB)并且希望每次只同步最新的更改。

我想写一个这样的脚本:

for each branch in githubRemote/branches do:
  if branch != otherRemote/branch:
     checkout githubRemote/branch
     push branch to otherRemote

【问题讨论】:

  • 为什么不在备份回购系统上运行git fetch origin? Git 已经负责只传输丢失的内容。如果这对您不起作用,可以提供有关您的确切设置/情况的更多信息。
  • 另一个远程在 TFS 2015 服务器上。我不认为我可以直接访问存储库
  • @FelixKling 关于设置您还需要了解什么?

标签: bash git shell tfs tfs-2015


【解决方案1】:

您可以在 powershell 脚本中查看以下示例:

git clone htpp://githubRemote/repoName -q #clone remote repository on github

cd repoName 

$branches = git branch -r  | foreach{ $_ -replace "^.*?\/", "" } | where {$_ -notmatch "HEAD" }
 
foreach($branch in $branches)
{
    git checkout $branch -q
    $message = git pull origin 

    if($message -ne "Already up to date.")
    {
      git push http://PAT@tfs2015:8081/tfs/DefaultCollection/project/_git/repo $branch
    
     #you can also use your username:password as credentials
     #if your password or username contain @ replace it with %40
     #git push http://username:password@tfs2015:8081/tfs/DefaultCollection/project/_git/repo $branch 
     
    }
 }

以上脚本将克隆远程 github 存储库并检查所有分支,然后从远程 github 存储库中提取最新代码。如果远程 github 分支发生变化,只有这些分支会被推送到另一个远程 tfs repo。

如果您使用您的 tfs 帐户用户名:密码作为凭据。您的帐户需要具有向远程 tfs 存储库做出贡献的权限。如果您没有访问权限,请让您的 tfs 项目管理员授予您权限。

您也可以要求您的 tfs 项目管理员提供具有代码读/写范围的 Personal access token(PAT)。然后您可以使用 PAT 作为凭据。

【讨论】:

  • $branches = git 分支 -r | foreach{ $_ -replace "^.*?\/", "" } | where {$_ -notmatch "HEAD" }' 它怎么知道从 github 远程检查更改?这意味着你每次运行脚本都需要克隆?
  • 另外,有没有办法检查 remoteA 是否与 remoteB 不同而无需结帐并拉动所有分支?正如我所说,这是一个非常重的存储库
  • 克隆命令只需要在第一次运行。 pull 命令检查来自 github 远程的更改。第一次克隆和拉取需要一些时间。之后拉命令将只拉变化,将花费更少的时间。如果您只想检查更改,您可能需要使用 checkout 然后拉取或获取。第一次无论哪种方式都需要很长时间,但之后会快得多。
  • 有没有比解析消息更安全的方法来检查是否存在差异(即 $message -ne "Already up to date.")?也许比较哈希值之类的?
【解决方案2】:

转到您的备份存储库并执行git fetch origingit pull origin

编辑:
我对 TFS 不太了解,但也许你可以在那里安排一个任务?
如果没有,可能它有一个 API 会触发 git pullgit fetch,因此您可以编写一个脚本调用正确的端点来执行此操作并将其安排在其他机器上。

【讨论】:

  • 在问题中查看我的 cmets 到 @FelixKling
【解决方案3】:

根据@Levi Lu-MSFT 的回答,我写了这个脚本:

git checkout master
git reset --hard

$branches = git branch -r  | foreach{ $_ -replace "^.*?\/", "" } | where {$_ -notmatch "HEAD" }
 
foreach($branch in $branches)
{
   
    $branchBackupHash = git rev-parse remoteTFS/$branch
    $branchWorkingHash = git rev-parse remoteGithub/$branch
    #check if the commit hash is the same
    if($branchBackupHash -ne $branchWorkingHash)
    {
      echo updating branch $branch
      git checkout origin/$branch -q
      git pull origin $branch
      git push remoteTFS $branch
    }
 }

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 2020-09-22
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2015-01-18
    • 2010-10-09
    相关资源
    最近更新 更多