-
-
Save ramosmerino/32c7ba288d13ff3f3c6cbe691763c73b to your computer and use it in GitHub Desktop.
Git Set Remote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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: | |
| # [email protected]:usuario/repo.git | |
| # ssh://[email protected]/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="[email protected]${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