Skip to content

Instantly share code, notes, and snippets.

@hhromic
Created November 15, 2018 10:40
Show Gist options
  • Select an option

  • Save hhromic/23f8cde99ac779e6046c016f9800653b to your computer and use it in GitHub Desktop.

Select an option

Save hhromic/23f8cde99ac779e6046c016f9800653b to your computer and use it in GitHub Desktop.

Revisions

  1. hhromic created this gist Nov 15, 2018.
    30 changes: 30 additions & 0 deletions gitmirror.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/usr/bin/env bash
    # Mirror a git repository into another
    # script by github.com/hhromic

    # origin git repository
    declare -r ORIGIN="<<your origin git repository here>>"

    # destination git repository (mirror)
    declare -r MIRROR="<<your destination (mirror) git repository here>>"

    # terminal color settings
    declare -r YLW=$(tput setaf 3 2>/dev/null)
    declare -r MGN=$(tput setaf 5 2>/dev/null)
    declare -r CYN=$(tput setaf 6 2>/dev/null)
    declare -r RST=$(tput sgr0 2>/dev/null)

    # make a temporary directory for mirroring
    echo "${YLW}Creating temporary working directory ...${RST}"
    TMPDIR=$(mktemp -d) || exit

    # clone origin, add mirror as remote and synchronise in mirror
    echo "${YLW}Mirroring '${MGN}${ORIGIN}${YLW}' into '${CYN}${MIRROR}${YLW}' ...${RST}"
    git clone --mirror "$ORIGIN" "$TMPDIR" || exit
    git --git-dir "$TMPDIR" remote add --mirror=push mirror "$MIRROR" || exit
    git --git-dir "$TMPDIR" fetch --prune origin || exit
    git --git-dir "$TMPDIR" push --mirror mirror || exit

    # clean temporary directory
    echo "${YLW}Cleaning temporary directory ...${RST}"
    rm -rf "$TMPDIR" || exit