This gist contains different ways to test git SSH operations using temporary SSH keys without modifying your ~/.ssh
directory.
For copy-paste interactive testing:
# 1. Create temp directory and SSH key
mkdir -p /tmp/git_ssh_test && cd /tmp/git_ssh_test
ssh-keygen -t ed25519 -f temp_key -N "" -C "temporary-test-key"
# 2. Show public key (add to GitHub)
cat temp_key.pub
# 3. Setup git SSH command (save original for CI safety)
[[ -n "${GIT_SSH_COMMAND+x}" ]] && ORIGINAL_GIT_SSH_COMMAND="$GIT_SSH_COMMAND" || ORIGINAL_GIT_SSH_COMMAND=""
export GIT_SSH_COMMAND="ssh -i $(pwd)/temp_key -o StrictHostKeyChecking=no"
# 4. Test SSH connectivity
ssh -T [email protected]
# 5. Test fastest git operation
git ls-remote [email protected]:username/repo.git
# 6. Test shallow clone
git clone --depth 1 [email protected]:username/repo.git test_clone
# 7. Cleanup (restore environment)
cd /tmp && rm -rf git_ssh_test
# Restore original GIT_SSH_COMMAND for CI safety
if [[ -n "${ORIGINAL_GIT_SSH_COMMAND+x}" ]]; then
[[ -n "$ORIGINAL_GIT_SSH_COMMAND" ]] && export GIT_SSH_COMMAND="$ORIGINAL_GIT_SSH_COMMAND" || unset GIT_SSH_COMMAND
fi
ssh -T [email protected]
: Tests SSH auth without git operationsgit ls-remote
: Fastest git operation - just lists referencesgit clone --depth 1
: Fast clone with minimal data transfer
- Copy the public key from
cat temp_key.pub
- Add it to GitHub β Settings β SSH and GPG keys
- Run the test commands
- Remove the key from GitHub when done
From our test:
- SSH auth: Instant
- git ls-remote: < 1 second
- Shallow clone: ~4 seconds (depends on repo size)
- No modification of
~/.ssh/
directory - Temporary key auto-cleaned up
- Perfect for CI/CD or testing scenarios
- No persistent SSH keys lying around
# If you have a private key as a string/file
echo "$PRIVATE_KEY_CONTENT" > /tmp/temp_key
chmod 600 /tmp/temp_key
export GIT_SSH_COMMAND="ssh -i /tmp/temp_key -o StrictHostKeyChecking=no"
# Skip host key checking completely
export GIT_SSH_COMMAND="ssh -i /path/to/key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
for repo in repo1 repo2 repo3; do
echo "Testing $repo..."
git ls-remote "[email protected]:username/$repo.git" || echo "Failed: $repo"
done
For automated testing in CI systems:
#!/bin/bash
# git_ssh_ci_test.sh - CI-safe git SSH testing
set -euo pipefail
REPO_URL="${1:-git@github.com:username/repo.git}"
TEMP_DIR="/tmp/git_ssh_test_$$"
SSH_KEY_PATH="$TEMP_DIR/ci_key"
cleanup() {
[[ -d "$TEMP_DIR" ]] && rm -rf "$TEMP_DIR"
if [[ -n "${ORIGINAL_GIT_SSH_COMMAND+x}" ]]; then
[[ -n "$ORIGINAL_GIT_SSH_COMMAND" ]] && export GIT_SSH_COMMAND="$ORIGINAL_GIT_SSH_COMMAND" || unset GIT_SSH_COMMAND
fi
}
trap cleanup EXIT
# Generate key and setup
mkdir -p "$TEMP_DIR"
ssh-keygen -t ed25519 -f "$SSH_KEY_PATH" -N "" -C "ci-test-key" >/dev/null
# Save and set GIT_SSH_COMMAND
[[ -n "${GIT_SSH_COMMAND+x}" ]] && ORIGINAL_GIT_SSH_COMMAND="$GIT_SSH_COMMAND" || ORIGINAL_GIT_SSH_COMMAND=""
export GIT_SSH_COMMAND="ssh -i $SSH_KEY_PATH -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
# Test operations
echo "Testing SSH auth..."
ssh -T [email protected] 2>/dev/null | grep -q "successfully authenticated"
echo "Testing git ls-remote..."
git ls-remote --heads "$REPO_URL" >/dev/null
echo "Testing shallow clone..."
git clone --depth 1 --quiet "$REPO_URL" "$TEMP_DIR/test_clone"
echo "β
All tests passed!"
Usage: ./git_ssh_ci_test.sh [email protected]:user/repo.git