Skip to content

Latest commit

 

History

History
202 lines (127 loc) · 5.69 KB

Git-on-Mac.md

File metadata and controls

202 lines (127 loc) · 5.69 KB

Guide to Setting Up Git on Mac and Connecting with GitHub using SSH

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.

1. Installing Git on Mac

Option 1: Install Git via Xcode Command Line Tools

Git comes pre-installed with the Xcode Command Line Tools. To install:

  1. Open Terminal (Cmd + Space, then type Terminal and press Enter).

  2. Run the following command:

    xcode-select --install
  3. A pop-up window will appear asking if you want to install the tools. Click "Install" to proceed.

Option 2: Install Git using Homebrew

Homebrew is a package manager for macOS that simplifies the installation of software.

  1. 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)"
  2. Once Homebrew is installed, install Git by running:

    brew install git
  3. Verify the installation by checking the version:

    git --version

    You should see something like git version 2.XX.X.

2. Configuring Git

After installing Git, you need to configure it with your identity. This is essential for tracking changes and attributing them to you.

  1. Set your username:

    git config --global user.name "Your Name"
  2. Set your email address (this should be the same email you use for GitHub):

    git config --global user.email "your_email@example.com"
  3. To view your configuration settings:

    git config --list

3. Generating SSH Key

SSH keys allow you to establish a secure connection between your computer and GitHub.

  1. Check for existing SSH keys:

    ls -al ~/.ssh

    If you see id_rsa and id_rsa.pub files, you already have an SSH key. You can skip the next step. If not, proceed to create a new SSH key.

  2. 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.

  3. (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.

  4. 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

4. Adding SSH Key to GitHub

  1. Copy the SSH key to your clipboard:

    pbcopy < ~/.ssh/id_rsa.pub
  2. Log in to your GitHub account.

  3. In the upper-right corner, click your profile photo, then click "Settings."

  4. In the left sidebar, click "SSH and GPG keys."

  5. Click "New SSH key" or "Add SSH key."

  6. In the "Title" field, add a descriptive label for the new key. For example, "My MacBook Pro."

  7. Paste your key into the "Key" field.

  8. Click "Add SSH key."

  9. Confirm your GitHub password if prompted.

5. Testing Your SSH Connection

To verify that your SSH key is correctly added and GitHub recognizes it:

  1. Run the following command in your terminal:

    ssh -T git@github.com
  2. You may see a warning like:

    The authenticity of host 'github.com (IP ADDRESS)' can't be established...
    

    Type yes to continue.

  3. 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.

6. Cloning a Repository using SSH

Now that Git is set up and connected to GitHub, you can clone repositories using SSH.

  1. Navigate to the repository on GitHub you want to clone.

  2. Click the green "Code" button.

  3. Ensure "SSH" is selected, then copy the SSH URL.

  4. In Terminal, navigate to the directory where you want to clone the repository.

  5. Run the following command:

    git clone git@github.com:username/repository.git

    Replace username with your GitHub username and repository with the repository name.

7. Common Git Commands

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> or git 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>

8. Additional Tips

  • 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 to main as the default branch name. You can configure Git to use main:

    git config --global init.defaultBranch main

Conclusion

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!