Skip to content

Instantly share code, notes, and snippets.

@ramosmerino
Created September 22, 2025 03:23
Show Gist options
  • Select an option

  • Save ramosmerino/32c7ba288d13ff3f3c6cbe691763c73b to your computer and use it in GitHub Desktop.

Select an option

Save ramosmerino/32c7ba288d13ff3f3c6cbe691763c73b to your computer and use it in GitHub Desktop.
Git Set Remote
#!/bin/bash
# Modifica el remote de un repositorio git para usarlo con config SSH diferentes
# Uso:
# gsr <ssh_host_alias>
# Ejemplo:
# gsre github.com-cristobalramos
set -e
# Verifica argumentos
if [ "$#" -ne 1 ]; then
echo "Uso: $0 <ssh_host_alias>"
echo "Ejemplo: $0 github.com-cristobalramos"
exit 1
fi
HOST_ALIAS="$1"
# Verifica que estamos en un repo Git
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "❌ Error: este directorio no es un repositorio Git"
exit 2
fi
# Obtiene la URL actual del remote origin
ORIGINAL_URL=$(git remote get-url origin)
# Extrae usuario/repositorio desde la URL actual
# Soporta formatos como:
# git@github.com:usuario/repo.git
# ssh://git@github.com/usuario/repo.git
REPO_PATH=$(echo "$ORIGINAL_URL" | sed -E 's#.*[:/](.+/.+)\.git#\1#')
if [ -z "$REPO_PATH" ]; then
echo "❌ No se pudo extraer el nombre del repositorio desde la URL: $ORIGINAL_URL"
exit 3
fi
# Construye la nueva URL
NEW_URL="git@github.com-${HOST_ALIAS}:${REPO_PATH}.git"
# Aplica el cambio
git remote set-url origin "$NEW_URL"
echo "✅ Remote 'origin' actualizado:"
echo " De: $ORIGINAL_URL"
echo " A : $NEW_URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment