This repository contains configurations for setting up a Git environment on MacOS using SSH and GPG keys. The configurations are tailored to different working directories where the Git settings change accordingly. This documentation will guide you through the process of generating and configuring SSH and GPG keys from scratch, and explain how to use the provided .gitconfig files and SSH configuration for a seamless development setup.
To generate a new SSH key, open your terminal and use the following command:
ssh-keygen -t ed25519 -C "username@example.com"If you are using a legacy system that doesn't support the Ed25519 algorithm, use:
ssh-keygen -t rsa -b 4096 -C "username@example.com"Follow the prompts to save the key to the default location (~/.ssh/id_ed25519 for Ed25519 keys). When prompted, you can set a passphrase for additional security.
Start the SSH agent in the background:
eval "$(ssh-agent -s)"Add your SSH private key to the SSH agent:
ssh-add ~/.ssh/id_ed25519For legacy systems:
ssh-add ~/.ssh/id_rsaTo generate a new GPG key, use the following command:
gpg --full-generate-keyFollow the prompts to configure your GPG key. Choose the following options when prompted:
- Kind of key: RSA and RSA
- Key size: 4096 bits
- Key expiration: Choose a suitable expiration period or leave it as "0" for no expiration
- Real name: Your Name
- Email address: username@example.com
- Comment: Optional
- Passphrase: Set a secure passphrase
After generating the key, list your keys to find the key ID:
gpg --list-secret-keys --keyid-format LONGExport the key:
gpg --armor --export your-key-idCreate the main .gitconfig file in your home directory (~/.gitconfig) with the following content:
[user]
name = Your Name
email = username@domain.com
signingkey = your-public-ssh-key-string
[commit]
gpgsign = true
[gpg]
format = ssh
[gpg "ssh"]
program = /Applications/1Password.app/Contents/MacOS/op-ssh-sign
[includeIf "gitdir:~/Sites/acme/"]
path = ~/Sites/acme/.gitconfig
[includeIf "gitdir:~/Sites/alpha/"]
path = ~/Sites/alpha/.gitconfigFor the acme directory (~/Sites/acme/.gitconfig):
[user]
email = username@acme.com
signingkey = your-gpg-key-string
[gpg]
format = openpgpFor the alpha directory (~/Sites/alpha/.gitconfig):
[user]
email = username@alpha.com
signingkey = your-gpg-key-string
[gpg]
format = openpgpCreate an SSH config file (~/.ssh/config) with the following content:
# Personal Use
Host github.com
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/your_id_rsa_private_key
# Acme
Host github_acme
HostName github.com
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/your_acme_id_rsa_private_key
# Alpha
Host github_alpha
HostName github.com
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/your_alpha_id_rsa_private_key
-
Clone the repository: Use your preferred method to clone the repository that contains this configuration setup.
-
Customize
.gitconfigand SSH configurations: Edit the.gitconfigand SSH config files with your own details. Replace placeholders likeyour-public-ssh-key-string,your-gpg-key-string, and paths with your actual information. -
Work with different directories: The provided configuration automatically applies different Git settings based on the working directory (
acmeandalpha). Ensure your directory structure matches the configuration (e.g.,~/Sites/acmeand~/Sites/alpha). -
Commit Signing: Your commits will be signed using the specified GPG key. Ensure your GPG key is added to your GitHub account by following GitHub's guide.
-
SSH Key Usage: Use the appropriate SSH key for different repositories by configuring your remote URLs with the corresponding host (
github_acmeorgithub_alpha).