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

时间:2020-07-30 11:09:49

标签: bash git shell tfs tfs-2015

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

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

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

3 个答案:

答案 0 :(得分: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存储库。

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

基于@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
    }
 }
相关问题