How to Add GitHub Credentials, Set Git Username, and Clone a Repository: A Step-by-Step Guide

Learn how to securely add GitHub credentials (HTTPS & SSH), set your Git username and email, and clone a repository to your local machine for smooth development workflows.

When working with GitHub, whether you’re a developer or a contributor, two fundamental tasks you’ll regularly encounter are authenticating yourself with GitHub and cloning repositories to your local environment. GitHub credentials allow you to securely access your repositories, while cloning enables you to work on your projects locally. Additionally, it’s important to configure your Git username and email to ensure your commits are properly attributed.

In this blog, we will explore:

  1. Setting up GitHub credentials (SSH and HTTPS).
  2. Configuring Git username and email.
  3. Cloning a GitHub repository.
  4. Managing authentication for easier use.

1. Setting Up GitHub Credentials

GitHub requires authentication to access repositories, especially for private ones. You can authenticate using two primary methods:

  • HTTPS Authentication (recommended for beginners).
  • SSH Authentication (for advanced users).

A. Using HTTPS for Authentication

This method is simple and recommended for those who prefer not to deal with SSH keys.

Step 1: Generating a Personal Access Token (PAT)

GitHub requires Personal Access Tokens (PAT) for authentication over HTTPS. Follow these steps to generate a PAT:

  1. Log in to GitHub: Go to GitHub.com and sign in.
  2. Go to Settings: Click your profile icon at the top-right and select Settings.
  3. Access Developer Settings: Scroll down to Developer Settings.
  4. Generate Personal Access Token: Under Personal access tokens, click Generate new token.
  5. Set Permissions: Name the token and choose scopes (permissions), like repo for repository access.
  6. Copy and Save Token: After generating the token, copy and store it securely.

Step 2: Using PAT for Git Operations

Use the PAT when prompted for your credentials during Git operations, like clone, push, or pull:

git clone https://github.com/username/repository.git
Username: your-username
Password: your-token

B. Using SSH for Authentication

SSH keys allow for passwordless authentication when working with GitHub.

Step 1: Generate an SSH Key

Run the following command to generate an SSH key:

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

Follow the prompts and optionally set a passphrase for added security.

Step 2: Add SSH Key to GitHub

  1. Copy the SSH Key: Use the following command to copy your public key: cat ~/.ssh/id_ed25519.pub
  2. Add Key to GitHub:
    • Go to GitHub Settings.
    • Under SSH and GPG keys, click New SSH key and paste your key.

Step 3: Test SSH Connection

Run the following command to test your SSH setup:

ssh -T git@github.com

If successful, you’ll see a message like: Hi username! You've successfully authenticated, but GitHub does not provide shell access.


2. Configuring Git Username and Email

Before you start committing code to your repositories, it’s important to set your Git username and email to ensure proper commit attribution.

A. Setting Global Username and Email

To apply your username and email across all repositories on your machine:

Set Global Username:

git config --global user.name "Your Name"

Set Global Email:

git config --global user.email "your_email@example.com"

B. Setting Local Username and Email

To set a different username and email for a specific repository, navigate to the project directory and run:

Set Local Username:

git config user.name "Your Name"

Set Local Email:

git config user.email "your_email@example.com"

C. Verifying Your Configuration

To check the configuration settings:

  • Global Config: git config --global --list
  • Local Config: git config --list

3. Cloning a GitHub Repository

Once your credentials are set, you can clone a repository to your local machine.

A. Cloning via HTTPS

  1. Find the HTTPS URL: Go to the repository on GitHub and copy the HTTPS URL by clicking the Code button.
  2. Clone the Repository: git clone https://github.com/username/repository.git
  3. Authenticate: When prompted, use your GitHub username and Personal Access Token.

B. Cloning via SSH

  1. Find the SSH URL: In the repository, copy the SSH URL.
  2. Clone the Repository: git clone git@github.com:username/repository.git

With SSH authentication configured, no password will be required during the cloning process.


4. Managing GitHub Authentication for Easier Use

A. Storing HTTPS Credentials with Git Credential Manager

You can store credentials securely to avoid entering them repeatedly:

  1. Install Git Credential Manager: If it’s not installed, download it from here.
  2. Cache Credentials: git config --global credential.helper cache

To cache credentials for a specific amount of time (e.g., one hour):

git config --global credential.helper 'cache --timeout=3600'

B. Automatically Using SSH for Cloning

Set SSH as your default protocol for cloning GitHub repositories:

git config --global url."git@github.com:".insteadOf "https://github.com/"

Now, even if you use the HTTPS link, Git will automatically translate it to SSH.


Conclusion

In this guide, we covered how to securely set up GitHub credentials using HTTPS and SSH, configure your Git username and email for proper commit attribution, and clone repositories to your local machine. Additionally, we looked at how to manage authentication for smoother workflows. With these tools in place, you’re ready to streamline your GitHub experience and contribute efficiently to your projects.

Happy coding!


Leave a Reply

Up ↑

Discover more from JD Bots

Subscribe now to keep reading and get access to the full archive.

Continue reading