Categories
GIT Terminal

Setting up multiple SSH keys on Mac

1) Generate a new SSH key using the secure Ed25519 algorithm:

ssh-keygen -t ed25519 -C "your_email@example.com"

For legacy systems that don’t support that algorithm (like AWS’ CodeCommit) use the following:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

2) When prompted, change the name of the file (key pair) if you want

3) Enter a strong passphrase

4) Repeat for as many keys as you need (for example as many GitHub, BitBucket, AWS or any other service that you want to use SSH keys with)

5) Copy the SSH public key and upload it to the respective service

pbcopy < ~/.ssh/id_ed25519.pub

6) Open your ~/.ssh/config file

open ~/.ssh/config

If the file doesn’t exist, create it like this (and then open it with the previous command):

touch ~/.ssh/config

7) Write your configuration so the machine understands you have multiple keys. The host has to be unique, the hostname is the service and the identity file is the key you’ve just created. Pay attention to the values that you will be filling. Here’s an example:

# Personal GitHub account
Host github.com
 HostName github.com
 User git
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa
# Work GitHub account
Host github.com-work
 HostName github.com
 User git
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa_work

8) Add each key to your machine’s SSH agent:

ssh-add -K ~/.ssh/id_ed25519

You can list all the keys added to the agent:

ssh-add -l

9) Clone your repositories in this manner, notice the -work after the domain, you set this value up previously in the config file:

git clone git@github.com-work:Homebrew/brew.git
Categories
GIT

Useful GIT Shortcuts

Branch and checkout out a new branch:

git checkout -b my-lovely-new-branch

Adding all files to stage:

git add -A

Check user info:

git config --list

Force a push (maybe after resetting a branch until a previous point in time):

git push origin your-lovely-branch --force
Categories
GIT

Useful GIT commands

Check the status of the repo

git status

List all the settings of the repository

git config --list

Set global (or local) account variables

git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"

Change the URI (URL) for a remote Git repository

git remote set-url origin git://new.url.here

Force Git to overwrite local files on pull

git fetch --all
git reset --hard origin/master

Delete untracked files

git clean -fd
Design a site like this with WordPress.com
Get started