Created
September 5, 2023 23:09
-
-
Save andresjesse/dfa0d5611016ca8508a97bad5ecd31b1 to your computer and use it in GitHub Desktop.
ssh-git-switch.py
This file contains 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
#!/usr/bin/python3 | |
# To install: | |
# chmod a+x {this file} | |
# sudo ln -s {path to this file} /usr/local/bin/ssh-git-switch.py | |
# For each ssh key in ~/.ssh, create a file: {private key}.config.txt, e.g. id_rsa.config.txt, with content: | |
# email@forthis.key | |
# Username | |
# Usage: | |
# In a git repository root folder, run $ssh-git-switch.py | |
import os, sys | |
from pathlib import Path | |
dirs = os.listdir(str(Path.home())+'/.ssh') | |
print("Select SSH Key for repository:") | |
for index, ssh_key in enumerate(dirs): | |
if ssh_key.endswith(".pub"): | |
print('\t', index, ssh_key) | |
x = int(input()) | |
config = open(str(Path.home())+'/.ssh/'+dirs[x][:-4]+'.config.txt', 'r').readlines() | |
email = config[0].strip() | |
username = config[1].strip() | |
cmd = 'git config core.sshCommand "ssh -i '+str(Path.home())+'/.ssh/'+dirs[x][:-4]+'"' | |
print('Executing:') | |
print('\t',cmd) | |
os.system(cmd) | |
cmd = 'git config user.name "'+username+'"' | |
print('Executing:') | |
print('\t',cmd) | |
os.system(cmd) | |
cmd = 'git config user.email "'+email+'"' | |
print('Executing:') | |
print('\t',cmd) | |
os.system(cmd) | |
cmd = 'cat .git/config' | |
print('Executing:') | |
print('\t',cmd) | |
os.system(cmd) | |
print("Reset author? (y/n)") | |
x = input() | |
if x == 'y': | |
cmd = 'git rebase -r --root --exec "git commit --amend --no-edit --reset-author"' | |
print('Executing:') | |
print('\t',cmd) | |
os.system(cmd) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment