繁体   English   中英

git 用另一个远程备份一个远程存储库

[英]git backup a remote repository with another remote

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

我想编写一个执行以下操作的脚本:

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

您可以在 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 存储库。

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

您还可以要求您的 tfs 项目管理员提供带有代码读/写 scope 的个人访问令牌(PAT)。 然后,您可以使用 PAT 作为凭据。

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

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

根据@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
    }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM