Created
April 20, 2026 04:53
-
-
Save kulcsarrudolf/99376c3fc94217d1e2cf3d30fcf8e325 to your computer and use it in GitHub Desktop.
Generate ~/.ssh/config for multiple Git accounts (personal GitHub + work GitHub + Bitbucket)
This file contains hidden or 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/env bash | |
| # Generate ~/.ssh/config for multiple Git accounts. | |
| # Covers: personal GitHub, work GitHub, and a Bitbucket account. | |
| # Adjust the key paths and host aliases to fit your setup. | |
| set -euo pipefail | |
| SSH_DIR="$HOME/.ssh" | |
| CONFIG_FILE="$SSH_DIR/config" | |
| mkdir -p "$SSH_DIR" | |
| chmod 700 "$SSH_DIR" | |
| # Back up any existing config before overwriting. | |
| if [ -f "$CONFIG_FILE" ]; then | |
| cp "$CONFIG_FILE" "$CONFIG_FILE.backup.$(date +%Y%m%d%H%M%S)" | |
| fi | |
| cat > "$CONFIG_FILE" <<'EOF' | |
| # Personal GitHub (default) | |
| Host github.com | |
| HostName github.com | |
| User git | |
| IdentityFile ~/.ssh/id_ed25519_personal | |
| IdentitiesOnly yes | |
| # Work GitHub (use: git@github-work:org/repo.git) | |
| Host github-work | |
| HostName github.com | |
| User git | |
| IdentityFile ~/.ssh/id_ed25519_work | |
| IdentitiesOnly yes | |
| # Bitbucket (use: git@bitbucket-work:org/repo.git) | |
| Host bitbucket-work | |
| HostName bitbucket.org | |
| User git | |
| IdentityFile ~/.ssh/id_ed25519_bitbucket | |
| IdentitiesOnly yes | |
| EOF | |
| chmod 600 "$CONFIG_FILE" | |
| echo "Wrote $CONFIG_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment