feat: git remote switch also reconfigures all branches

This commit is contained in:
janishutz committed 2026-10-05 17:28:58 +02:00
1 parent 586bcca9d8
commit 5764b73e5c
1 file changed
+13 -15
+13 -15
View File
@@ -3,21 +3,19 @@
set -e set -e
origin=$(git remote get-url origin) origin=$(git remote get-url origin)
git_branch=$(git branch | grep '*' | tr -d ' *')
if [[ $(echo "$origin" | awk -e '/https/') != "" ]]; then if [[ $(echo "$origin" | awk -e '/https/') != "" ]]; then
git_host=$(echo "$origin" | cut -d "/" -f 3) git_url="git@$(echo "$origin" | cut -d "/" -f 3):/$($(echo "$origin" | cut -d "/" -f 4-))"
git_repo=$(echo "$origin" | cut -d "/" -f 4-)
git remote remove origin
git remote add origin git@$git_host:/$git_repo
git fetch
git branch --set-upstream-to=origin/$git_branch $git_branch
echo "Updated to SSH url git@$git_host:/$git_repo, for branch $git_branch"
else else
git_host=$(echo "$origin" | cut -d "@" -f 2 | cut -d ":" -f 1) git_url="https://$(echo "$origin" | cut -d "@" -f 2 | cut -d ":" -f 1)/$(echo "$origin" | cut -d ":" -f 2 | cut -d "/" -f 2-)"
git_repo=$(echo "$origin" | cut -d ":" -f 2 | cut -d "/" -f 2-)
git remote remove origin
git remote add origin https://$git_host/$git_repo
git fetch
git branch --set-upstream-to=origin/$git_branch $git_branch
echo "Updated to HTTPS url https://$git_host/$git_repo, for branch $git_branch"
fi fi
git_branches=$(git branch | grep '*' | tr -d ' *')
git remote remove origin
git remote add origin $git_url
git fetch
echo "New git url $git_url, updating branches"
for branch in $git_branches; do
git checkout $branch
git branch --set-upstream-to=origin/$branch $branch
echo "Branch $branch configured to track on origin/$branch"
done