Git SSH Setup for Mac M1/M2

Git SSH Setup for Mac M1/M2

In the world of software development, Git is the go-to version control system for managing source code. Securely connecting to your Git repositories is essential. One of the most secure methods to do this is by using SSH (Secure Shell) keys. In this guide, we will walk through the steps to set up Git with SSH on a Mac.

1. Installing Git

The first step is to ensure Git is installed on your Mac. If it's not already installed, you can use Homebrew, a package manager for macOS, to install it. Open your Terminal and run the following commands.

brew update
brew install git

2. Generating an SSH Key

Before you can use SSH with Git, you need an SSH key. If you don't have one, you can generate it with the following command, replacing your email address.

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

This command will prompt you to save the key to a specific location. You can press Enter to accept the default location.

3. Adding the SSH Key to ssh-agent

To ensure your SSH key is available and secure, you should add it to the ssh-agent. The ssh-agent is a program that holds private keys used for public key authentication. Use these commands to add your SSH key.

eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

If you used a different file name for your SSH key, replace id_ed25519 with your key's file name.

4. Customizing SSH Configuration

nano ~/.ssh/config

Then, add the following lines to the config file, specifying your own IdentityFile if necessary.

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

5. Add the SSH Key to Your Git Account

Log in to your Git hosting provider (e.g., GitHub, GitLab) and go to your account settings. In the SSH keys or SSH and GPG keys section, add your SSH key. You can copy the public key to your clipboard using this command.

pbcopy < ~/.ssh/id_ed25519.pub
        or
cat ~/.ssh/id_ed25519.pub

Then, paste the public key command response into the SSH key field on your Git provider's website.

6. Testing the SSH Connection

To make sure your SSH connection is set up correctly, run the following command:

ssh -T git@github.com

Replace github.com with your Git hosting provider's domain if you're not using GitHub. You should see a success message if your SSH key is correctly configured.

7. Conclusion

Setting up Git with SSH on your Mac is a crucial step in ensuring the security and efficiency of your software development workflow. By following these steps, you can take advantage of the enhanced security and convenience provided by SSH keys. Whether you're working on a personal project or collaborating on a team, this setup will make your Git experience smoother and more secure.


Also, let’s connect on LinkedIn! Here’s my profile link: LinkedIn.

Stay tuned for more articles. Until then, happy coding!

Did you find this article valuable?

Support Rahul Yarragodula by becoming a sponsor. Any amount is appreciated!