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

lf3rwulv  于 2022-11-16  发布在  Shell
关注(0)|答案(3)|浏览(193)

我在github上有一个远程仓库和另一个远程仓库用于备份。由于仓库非常大,我不想每次都使用git push --mirror(它超过20 GB),并且希望每次只同步最新的更改。
我想写一个脚本,它的作用是这样的:

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

ufj5ltwl1#

您可以在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仓库并 checkout 所有的分支,然后从远程github仓库提取最新的代码。如果对远程github分支进行了更改,则只有这些分支会被推送到其他远程tfs仓库。
如果您使用tfs帐户username:password作为凭据。您的帐户需要具有向远程tfs存储库提供内容的权限。如果您没有该权限,请要求tfs项目管理员授予您该权限。
您也可以要求您的tfs项目管理员给予具有程式码读取/写入范围的个人存取权杖(PAT)。然后您就可以使用PAT做为认证。

gc0ot86w

gc0ot86w2#

转到您的备份存储库并执行git fetch origingit pull origin
编辑:
我不太了解TFS,但也许,你可以在那里安排一个任务?
如果没有,也许它有一个API将触发一个git pullgit fetch,所以你可以写一个脚本调用正确的端点来做这件事,并安排它在其他一些机器上。

anhgbhbe

anhgbhbe3#

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

相关问题