Connect with us

Git

How to Log In to GitHub from Terminal?

Spread the love

For developers, logging in to GitHub from the terminal is a crucial step in managing repositories, pushing code, and collaborating on projects. This guide provides a clear, step-by-step approach to securely authenticate your GitHub account using the terminal.

Why Log In to GitHub from the Terminal?

Using the terminal to log in to GitHub offers several advantages:

  1. Seamless Integration: Enables direct communication between your local repositories and GitHub.
  2. Enhanced Productivity: Simplifies workflows for cloning, pulling, and pushing code.
  3. Security: Leverages secure authentication mechanisms like personal access tokens (PATs) or SSH keys.

Prerequisites

Before you begin, ensure that:

  • Git is installed on your system. You can verify this by running:
  git --version
  • You have a GitHub account.
  • You have terminal or command prompt access.

Step-by-Step Instructions

1. Choose an Authentication Method

GitHub supports two primary authentication methods for terminal access:

  • Personal Access Token (PAT): A token-based method replacing username/password authentication.
  • SSH Keys: A key-based method for secure, password-less authentication.

Choose the method that best suits your workflow. Below are steps for both methods.


Method 1: Using a Personal Access Token (PAT)

Step 1: Generate a PAT

  1. Log in to your GitHub account.
  2. Navigate to Settings > Developer Settings > Personal Access Tokens.
  3. Click Generate new token and select appropriate scopes (e.g., repo for repository access).
  4. Copy the token and store it securely. You won’t be able to view it again.

Step 2: Log In Using the PAT

When prompted for your username and password during Git operations (e.g., git clone, git push):

  1. Use your GitHub username as the username.
  2. Use the PAT as the password.

For example:

Username: your-github-username
Password: your-personal-access-token

To avoid repeated prompts, you can cache credentials using Git:

git config --global credential.helper cache

Method 2: Using SSH Keys

Step 1: Generate an SSH Key

  1. Open your terminal and run:
   ssh-keygen -t ed25519 -C "[email protected]"

Replace [email protected] with your GitHub email address.

  1. Follow the prompts to save the key pair. By default, keys are stored in ~/.ssh/.
  2. Add the private key to your SSH agent:
   eval "$(ssh-agent -s)"
   ssh-add ~/.ssh/id_ed25519

Step 2: Add the SSH Key to GitHub

  1. Copy the public key to your clipboard:
   cat ~/.ssh/id_ed25519.pub
  1. Log in to GitHub and navigate to Settings > SSH and GPG Keys.
  2. Click New SSH Key, paste the public key, and save.

Step 3: Test the SSH Connection

Verify the connection with GitHub:

ssh -T [email protected]

You should see a success message.


Additional Tips

  • Check Your Authentication Method: To confirm your setup, try cloning a repository:
  git clone [email protected]:username/repo.git  # For SSH
  git clone https://github.com/username/repo.git  # For PAT
  • Keep Credentials Secure: Use tools like password managers or encrypted vaults for storing PATs and SSH keys.
  • Rotate Tokens and Keys Regularly: Periodically update PATs and regenerate SSH keys to enhance security.

Conclusion

Logging in to GitHub from the terminal is a fundamental skill that streamlines your development workflow. Whether you choose PATs or SSH keys, both methods offer secure and efficient ways to interact with your repositories. By following this guide, you’ll be equipped to handle GitHub authentication confidently and effectively.


Spread the love
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *