Last active
December 5, 2025 18:45
-
-
Save kntjspr/5eb2cd3def39f3b4dbdf573a0f36e45e to your computer and use it in GitHub Desktop.
Setup git alias to configure git per repo
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
| #!/bin/bash | |
| # --- INSTALL WRAPPER --- | |
| INSTALL_DIR="$HOME/.local/bin" | |
| INSTALL_PATH="$INSTALL_DIR/git-setup" | |
| BASHRC="$HOME/.bashrc" | |
| mkdir -p "$INSTALL_DIR" | |
| if [ "$(basename "$0")" != "git-setup" ]; then | |
| echo "Installing git-setup to $INSTALL_PATH..." | |
| cp "$0" "$INSTALL_PATH" | |
| chmod +x "$INSTALL_PATH" | |
| if ! grep -q "alias git-setup=" "$BASHRC"; then | |
| echo "alias git-setup='$INSTALL_PATH'" >> "$BASHRC" | |
| echo "β Alias added to $BASHRC" | |
| fi | |
| echo "β Installation complete. Reload shell: source $BASHRC" | |
| exit 0 | |
| fi | |
| # --- CONFIGURATION --- | |
| SSH_DIR="$HOME/.ssh" | |
| REMOTE_NAME="dev" | |
| SSH_CONFIG="$SSH_DIR/config" | |
| GPG_KEY_ID="8FED9D2E4BA47F0156030740980F354B5EEF0B51" | |
| mkdir -p "$SSH_DIR" | |
| chmod 700 "$SSH_DIR" | |
| # --- SSH KEY SETUP --- | |
| if grep -q "^Host github-${REMOTE_NAME}" "$SSH_CONFIG" 2>/dev/null; then | |
| SSH_KEY_PATH=$(grep -A 3 "^Host github-${REMOTE_NAME}" "$SSH_CONFIG" | grep "IdentityFile" | awk '{print $2}') | |
| SSH_PUB_PATH="${SSH_KEY_PATH}.pub" | |
| echo "β 'Host github-${REMOTE_NAME}' configuration already exists" | |
| echo "π Verifying SSH key..." | |
| if [ -f "$SSH_KEY_PATH" ]; then | |
| echo "β SSH key verified: $SSH_KEY_PATH" | |
| else | |
| echo "β SSH key file missing: $SSH_KEY_PATH" | |
| exit 1 | |
| fi | |
| else | |
| SSH_KEY_PATH="$SSH_DIR/id_${REMOTE_NAME}_ed25519" | |
| SSH_PUB_PATH="${SSH_KEY_PATH}.pub" | |
| echo "SSH key for $REMOTE_NAME not found." | |
| echo "Generate or Import SSH key? (g/i/cancel)" | |
| read -r ssh_action | |
| case "$ssh_action" in | |
| g) | |
| ssh-keygen -t ed25519 -C "$REMOTE_NAME@github" -f "$SSH_KEY_PATH" -N "" || { echo "β SSH key generation failed"; exit 1; } | |
| chmod 600 "$SSH_KEY_PATH" | |
| ;; | |
| i) | |
| echo "Enter path to private key file:" | |
| read -r key_file_path | |
| if [ ! -f "$key_file_path" ]; then | |
| echo "β File not found"; exit 1 | |
| fi | |
| cat "$key_file_path" | dos2unix 2>/dev/null > "$SSH_KEY_PATH" || cat "$key_file_path" > "$SSH_KEY_PATH" | |
| chmod 600 "$SSH_KEY_PATH" | |
| ;; | |
| *) | |
| echo "β SSH key required. Exiting."; exit 1 | |
| ;; | |
| esac | |
| [ ! -f "$SSH_PUB_PATH" ] && ssh-keygen -y -f "$SSH_KEY_PATH" > "$SSH_PUB_PATH" && chmod 644 "$SSH_PUB_PATH" | |
| fi | |
| # --- VERIFY SSH --- | |
| echo "π Testing SSH connection to GitHub..." | |
| GIT_SSH_COMMAND="ssh -i $SSH_KEY_PATH -o IdentitiesOnly=yes" ssh -T git@github.com || { echo "β SSH failed"; exit 1; } | |
| echo "β SSH authentication successful!" | |
| # --- GPG SETUP --- | |
| if ! gpg --list-secret-keys "$GPG_KEY_ID" >/dev/null 2>&1; then | |
| echo "β GPG key $GPG_KEY_ID not found in keyring" | |
| echo "Import your key now? (provide file path or cancel)" | |
| read -r gpg_file | |
| if [ -f "$gpg_file" ]; then | |
| gpg --import "$gpg_file" || { echo "β Failed to import GPG"; exit 1; } | |
| else | |
| echo "β No GPG key provided. Exiting."; exit 1 | |
| fi | |
| fi | |
| echo "β GPG key ready: $GPG_KEY_ID" | |
| # --- LOCAL GIT CONFIG --- | |
| read -p "Enter Git name for this repo: " GIT_NAME | |
| git config user.name "$GIT_NAME" | |
| read -p "Enter Git email for this repo: " GIT_EMAIL | |
| git config user.email "$GIT_EMAIL" | |
| git config commit.gpgsign true | |
| git config user.signingkey "$GPG_KEY_ID" | |
| echo "β Local Git configured: name, email, signing key" | |
| # --- VERIFY COMMIT SIGNING --- | |
| echo "π Testing commit signing..." | |
| TMP_REPO=$(mktemp -d) | |
| cd "$TMP_REPO" || exit 1 | |
| git init | |
| git commit --allow-empty -m "Test commit" || { echo "β Commit signing failed"; exit 1; } | |
| cd - >/dev/null | |
| rm -rf "$TMP_REPO" | |
| echo "β Commit signing works" | |
| # --- FINISH --- | |
| echo "" | |
| echo "π Setup complete. You can now run 'git push origin main' normally in this repo." |
Author
Author
Pulling private repositories:
mkdir test-repo
cd test-repo
git init
git-setup # runs your setup script, sets SSH/GPG locally
git remote add origin git@github.com:username/private-repo.git
git pull origin main
Author
git config commit.gpgsign true
git config user.signingkey
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
chmod +x git-setup.sh
./git-setup.sh
source ~/.bashrc
You can now run git-setup on any directory to setup git and push with gpg verified