Skip to content

Instantly share code, notes, and snippets.

@Victorcorcos
Last active February 13, 2020 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Victorcorcos/b88ae6beeda0ee7a1aa5dd6152cea74a to your computer and use it in GitHub Desktop.
Save Victorcorcos/b88ae6beeda0ee7a1aa5dd6152cea74a to your computer and use it in GitHub Desktop.
Creating a mirror repository on git

Creating a Mirror Repository

And submit the changes from the original repository to the mirrored repository.

These are shell functions to

  1. Create a git mirror repository from a original repository (create_mirror)
  2. Update the mirror repository with the original repository changes (update_mirror)

You can add to your ~/.bash_profile the following functions

Shell functions

# Initialize the repository to mirror
## Inputs: source_repository, target_repository
# Example: create_mirror git@github.com:source-profile/source-repository.git git@github.com:target-profile/target-repository.git
create_mirror() {
  # Initializing the mirror.
  git clone --mirror $1
  cd `echo $1 | rev | cut -d '/' -f 1 | rev`
  git remote set-url --push origin $2

  # Updating the Mirror
  git fetch -p origin
  git show-ref | cut -d ' ' -f2 | grep 'pull' | xargs -L1 git update-ref -d # Delete PRs to let the push mirror work
  git push --mirror
}

# Pushing changes to the mirror repository (Need to be inside the initialized repository)
## Inputs: none
# Example: update_mirror
update_mirror() {
  git fetch -p origin
  git show-ref | cut -d ' ' -f2 | grep 'pull' | xargs -L1 git update-ref -d # Delete PRs to let the push mirror work
  git push --mirror
}

Usage

First, you need to create a blank repository, it will be used as a target repository to mirror the original repository.

Then, just execute the following command on the shell

create_mirror git@github.com:source-profile/source-repository.git git@github.com:target-profile/target-repository.git

And see the magic happen!

Later on, if you want to submit new commits from the original-repository to the target-repository, you just need to enter on the created /original-repository folder and execute the following command

update_mirror

And that's it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment