Skip to content

Instantly share code, notes, and snippets.

@kntjspr
Last active December 5, 2025 18:45
Show Gist options
  • Select an option

  • Save kntjspr/5eb2cd3def39f3b4dbdf573a0f36e45e to your computer and use it in GitHub Desktop.

Select an option

Save kntjspr/5eb2cd3def39f3b4dbdf573a0f36e45e to your computer and use it in GitHub Desktop.
Setup git alias to configure git per repo
#!/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."
@kntjspr
Copy link
Author

kntjspr commented Dec 5, 2025

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