Files
dotfiles/.config/zsh/autoload/git/git-https-and-ssh
Julian Prein 73c0864c8b git: Add https-and-ssh to setup origin
Sets up origin so that it fetches over https but pushes over ssh. This
way unlocking the ssh key is only needed when really necessary.
2022-12-01 01:27:28 +01:00

21 lines
588 B
Bash
Executable File

#!/bin/sh
#!/usr/bin/zsh
# Sets up origin so that it fetches over https but pushes over ssh. This way
# unlocking the ssh key is only needed when really necessary.
GIT="git"
origin="$($GIT remote -v | grep -Po '(?<=^origin\t).*(?= \(fetch\)$)')"
if [ "${origin##https://}" = "$origin" ]; then
https_origin="${origin##*@}"
https_origin="https://${https_origin/://}"
$GIT remote set-url origin "$https_origin"
$GIT remote set-url --push origin "$origin"
else
ssh_origin="${origin##https://}"
ssh_origin="git@${ssh_origin/\//:}"
$GIT remote set-url --push origin "$ssh_origin"
fi