fix-git.sh 933 B

1234567891011121314151617181920212223242526272829303132
  1. #!/bin/bash
  2. OLD_URL=$(git remote get-url origin)
  3. if [[ $OLD_URL == "https://github.com"* ]]; then
  4. # Modified sed pattern to better handle GitHub URLs
  5. USER_REPO=$(echo $OLD_URL | sed -n 's|https://github.com/\([^/]*/[^/]*\)|\1|p')
  6. if [ -z "$USER_REPO" ]; then
  7. echo "Error: Could not extract user/repo from URL: $OLD_URL"
  8. exit 1
  9. fi
  10. # Remove any trailing .git if present and add it back consistently
  11. USER_REPO=$(echo $USER_REPO | sed 's/\.git$//')
  12. NEW_URL="git@github.com:${USER_REPO}.git"
  13. echo "Old URL: $OLD_URL"
  14. echo "New URL: $NEW_URL"
  15. # Update the remote URL to use SSH
  16. git remote set-url origin "$NEW_URL"
  17. # Verify the change
  18. CURRENT_URL=$(git remote get-url origin)
  19. if [ "$CURRENT_URL" = "$NEW_URL" ]; then
  20. echo "Successfully updated remote URL to use SSH."
  21. else
  22. echo "Error: Failed to update remote URL."
  23. exit 1
  24. fi
  25. else
  26. echo "Remote URL is already using SSH or a different protocol: $OLD_URL"
  27. fi