Skip to main content

git/_ensure_ssh_key_loaded

1#!/usr/bin/env bash
2# This script should run before any Git operations that interact with
3# a remote; they ensure my SSH key is loaded by the macOS Keychain before
4# trying to push/pull anything.
6set -o errexit
7set -o nounset
9if [[ $(uname) == "Darwin" ]]; then
10 has_ssh_identity_loaded() {
11 ssh-add -l >/dev/null
12 }
14 if ! has_ssh_identity_loaded
15 then
16 ssh-add --apple-use-keychain $(find ~/.ssh -name 'id_*' | grep -v '.pub$' | head -n 1)
17 fi
18fi