This guide will walk you through setting up Git on a Mac and connecting it to your GitHub account using SSH keys. This setup ensures a secure and efficient workflow, especially for developers working with Git repositories frequently.
Git comes pre-installed with the Xcode Command Line Tools. To install:
-
Open Terminal (
Cmd + Space
, then typeTerminal
and pressEnter
). -
Run the following command:
xcode-select --install
-
A pop-up window will appear asking if you want to install the tools. Click "Install" to proceed.
Homebrew is a package manager for macOS that simplifies the installation of software.
-
First, ensure Homebrew is installed. If not, install it by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Once Homebrew is installed, install Git by running:
brew install git
-
Verify the installation by checking the version:
git --version
You should see something like
git version 2.XX.X
.
After installing Git, you need to configure it with your identity. This is essential for tracking changes and attributing them to you.
-
Set your username:
git config --global user.name "Your Name"
-
Set your email address (this should be the same email you use for GitHub):
git config --global user.email "your_email@example.com"
-
To view your configuration settings:
git config --list
SSH keys allow you to establish a secure connection between your computer and GitHub.
-
Check for existing SSH keys:
ls -al ~/.ssh
If you see
id_rsa
andid_rsa.pub
files, you already have an SSH key. You can skip the next step. If not, proceed to create a new SSH key. -
Generate a new SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-t rsa
: Specifies the type of key to create (RSA).-b 4096
: Specifies the length of the key (4096 bits for security).-C "your_email@example.com"
: Adds a label to the key.
When prompted to "Enter a file in which to save the key," press
Enter
to accept the default location. -
(Optional) Secure your SSH key with a passphrase:
You will be asked to enter a passphrase. This adds an extra layer of security. You can leave it blank if you prefer.
-
Add your SSH key to the ssh-agent:
First, ensure the ssh-agent is running:
eval "$(ssh-agent -s)"
Then, add your SSH key to the ssh-agent:
ssh-add -K ~/.ssh/id_rsa
-
Copy the SSH key to your clipboard:
pbcopy < ~/.ssh/id_rsa.pub
-
Log in to your GitHub account.
-
In the upper-right corner, click your profile photo, then click "Settings."
-
In the left sidebar, click "SSH and GPG keys."
-
Click "New SSH key" or "Add SSH key."
-
In the "Title" field, add a descriptive label for the new key. For example, "My MacBook Pro."
-
Paste your key into the "Key" field.
-
Click "Add SSH key."
-
Confirm your GitHub password if prompted.
To verify that your SSH key is correctly added and GitHub recognizes it:
-
Run the following command in your terminal:
ssh -T git@github.com
-
You may see a warning like:
The authenticity of host 'github.com (IP ADDRESS)' can't be established...
Type
yes
to continue. -
If everything is set up correctly, you should see:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
This confirms that your SSH key is correctly linked to your GitHub account.
Now that Git is set up and connected to GitHub, you can clone repositories using SSH.
-
Navigate to the repository on GitHub you want to clone.
-
Click the green "Code" button.
-
Ensure "SSH" is selected, then copy the SSH URL.
-
In Terminal, navigate to the directory where you want to clone the repository.
-
Run the following command:
git clone git@github.com:username/repository.git
Replace
username
with your GitHub username andrepository
with the repository name.
Here are a few essential Git commands to get you started:
- Clone a repository:
git clone <repository_url>
- Check the status of your repository:
git status
- Add changes to be committed:
git add <file_name>
orgit add .
(adds all changes) - Commit changes:
git commit -m "Commit message"
- Push changes to GitHub:
git push origin <branch_name>
- Pull the latest changes from GitHub:
git pull origin <branch_name>
-
Global Ignore File: Set up a global
.gitignore
file to avoid committing unwanted files (e.g.,.DS_Store
):git config --global core.excludesfile ~/.gitignore_global echo ".DS_Store" >> ~/.gitignore_global
-
Set Default Branch Name: GitHub has moved from
master
tomain
as the default branch name. You can configure Git to usemain
:git config --global init.defaultBranch main
This guide has covered the basics of setting up Git on a Mac, connecting it to your GitHub account using SSH keys, and performing basic Git operations. With this setup, you can now securely manage your code repositories and collaborate effectively with others.
Happy coding!