bashgitshelltfstfs-2015

git backup a remote repository with another remote


I have a remote repository on github and another remote repository for backup. Since the repository is very big I don't want everytime to use git push --mirror (it is above 20GB) and want to sync only latest changes everytime.

I want to write a script that is doing something like this:

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

Solution

  • based on @Levi Lu-MSFT's answer, I wrote this script:

    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
        }
     }