Search code examples
gitmirroring

Automatically mirror git repository


I'm have got a git I access via ssh. I'd like to backup the server in another to location by mirroring. I've got another server where I can setup another git repository.

My idea is: Forward in some way the commit from server 1 to server 2 automatically, is it possible?


Solution

  • To automatically carry out actions after a push was performed on a repository the post-receive hook can be used. It will be called once the entire pushing process is finished.

    You can push to the other server by simply using a standard git push with the --mirror [1] option in there:

    #!/bin/bash
    git push --mirror [email protected]:mirror.git
    

    [1]

       --mirror
           Instead of naming each ref to push, specifies that all refs under
           refs/ (which includes but is not limited to refs/heads/,
           refs/remotes/, and refs/tags/) be mirrored to the remote
           repository. Newly created local refs will be pushed to the remote
           end, locally updated refs will be force updated on the remote end,
           and deleted refs will be removed from the remote end. This is the
           default if the configuration option remote.<remote>.mirror is set.