Config: Use Second Github Account Easily

Posted by : on

Category : powershell   scripts   network


Information on I use one account on my server, that commits as different github users/accounts

Generate a second ssh key

The ssh-keygen program is a versatile tool used to generate, manage, and convert SSH key pairs. Below is a summary of its commonly used options and their purposes:

Common Options

Option Description
-t type Specifies the type of key to create. Common types: rsa, dsa, ecdsa, ed25519.
-b bits Sets the key length in bits. Example: -b 4096 for RSA.
-C comment Adds a comment (e.g., email address) to the key.
-f filename Specifies the output filename for the private key. Example: -f ~/.ssh/id_rsa.
-N passphrase Sets a passphrase for the private key. Leave empty (-N "") for no passphrase.
-q Suppresses output (quiet mode).
-y Reads a private key and outputs the corresponding public key.
-p Changes the passphrase of an existing private key.
-l Shows the fingerprint of a key.
-e Converts an OpenSSH public key to RFC 4716 format.
-m format Specifies the key format. Use PEM or RFC4716.
-o Saves the private key using the new OpenSSH format (more secure).
-a rounds Specifies the number of KDF rounds when saving a private key with a passphrase.

Key Types Available

Type Description
rsa RSA algorithm (supports long keys but slower).
dsa Digital Signature Algorithm (legacy, not recommended).
ecdsa Elliptic Curve Digital Signature Algorithm (faster, more secure).
ed25519 Ed25519 elliptic curve algorithm (fast and highly secure).

Notes:

  • RSA: The higher the bit value, the more secure the key (but slower). 2048 or 3072 bits is usually recommended.
  • DSA: Limited to 1024 bits, making it less secure. It is now considered deprecated.
  • ECDSA & Ed25519: The bit length is related to the curve used. For Ed25519, the bit length is fixed at 256 bits.

Examples

Generate an Ed25519 key (recommended for most use cases)

ssh-keygen -t ed25519 -C "user@example.com" -N "" -f ~/.ssh/id_ed25519**_other**

Add Key in Github

key1

Add SSH Key in .ssh/config file

nano ~/.ssh/config

Add this section

Host github-other
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_other

Clone a Repo and Set Remote

To use the new key and bypass the one used by defaults, change the remote for the one set in the ssh config file:

remote

Then change the git username and email from inside the cloned directory

git config user.name "second name"
git config user.email "second email"

You can use the script below.


#!/bin/bash

# Ensure the script exits on any error
set -e

# Check if a repository URL is provided
if [ -z "$1" ]; then
    echo "Usage: $0 <repository_url>"
    exit 1
fi

# Store the repository URL
REPO_URL="$1"

# Extract the repository name from the URL
REPO_NAME=$(basename -s .git "$REPO_URL")

# Clone the repository
git clone "$REPO_URL"

# Get the full path of the cloned directory
FULL_PATH=$(realpath "$REPO_NAME")

# Output the full path
echo "Cloned repository path: $FULL_PATH"

pushd "$FULL_PATH" > /dev/null

# Check if the current directory is a Git repository
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    echo "Error: This is not a Git repository."
    exit 1
fi

# Fetch the current remote URL
REMOTE_URL=$(git remote get-url origin)

# Replace 'github.com' with 'github-other'
MODIFIED_URL=${REMOTE_URL/github.com/github-other}

# Output the modified URL
echo "Modified remote URL: $MODIFIED_URL"

git remote set-url origin "$MODIFIED_URL"

popd  > /dev/null

About Guillaume Plante
Guillaume Plante

A developper with a passion for technology, music, astronomy and art. Coding range: hardware/drivers, security, ai,. c/c++, powershell

Email : guillaumeplante.qc@gmail.com

Website : https://arsscriptum.ddns.net

Useful Links