Git
How to Log In to GitHub from the Terminal?
Logging into GitHub from the terminal is a crucial skill for developers who want to push, pull, and manage their repositories directly from the command line. GitHub offers both HTTPS and SSH methods for authentication, each with its own setup process. In this blog, we’ll walk you through how to authenticate GitHub from the terminal, covering both methods.
1. Why Authenticate GitHub from the Terminal?
Authenticating GitHub from the terminal allows you to:
- Access and manage repositories directly: Perform Git operations (e.g., cloning, pushing, pulling) without entering credentials repeatedly.
- Streamline workflows: Especially useful for developers who work with Git and GitHub in CI/CD environments or use automation scripts.
- Use secure connections: With SSH keys or personal access tokens (PATs), you can authenticate securely without sharing your GitHub password.
2. Authentication Options
GitHub offers two main ways to authenticate via the terminal:
- HTTPS with a Personal Access Token (PAT): Replaces password-based authentication with a more secure token.
- SSH Keys: Enables secure login through cryptographic keys, eliminating the need for a password or token.
Both methods have their benefits. SSH is recommended for frequent users, while HTTPS is often easier for quick tasks.
3. Authenticating GitHub with a Personal Access Token (HTTPS Method)
Using a Personal Access Token (PAT) is a secure way to authenticate with GitHub over HTTPS. GitHub no longer supports password-based authentication, so PATs are required.
Step 1: Generate a Personal Access Token on GitHub
- Log in to GitHub and navigate to Settings.
- Under Developer settings, click on Personal access tokens.
- Click on Generate new token and set a name (e.g., “CLI Access”).
- Set an expiration date or choose No expiration if desired (be cautious with long-expiration tokens).
- Choose the scopes (permissions) you need. For basic repo operations, select:
repo
for repository access.workflow
for GitHub Actions.write:packages
for managing packages (optional).
- Click Generate token and copy the token somewhere secure—you won’t be able to view it again.
Step 2: Log In to GitHub with the PAT in the Terminal
Once you have the token, you can use it to authenticate with Git commands that require login.
Example: Clone a Repository with HTTPS
- Copy the repository URL from GitHub. Make sure it’s the HTTPS version (e.g.,
https://github.com/username/repository.git
). - Run the following command to clone the repository:
git clone https://github.com/username/repository.git
- When prompted for your username, enter your GitHub username.
- When prompted for your password, paste the PAT instead of your GitHub password.
Example: Push to a Repository
Once authenticated, Git will remember the PAT, allowing you to push without entering it each time. Use:
git push origin main
You’re now logged in via HTTPS and can use Git commands without re-entering credentials.
4. Authenticating GitHub with SSH Keys (SSH Method)
The SSH method is ideal for frequent users and provides a secure, password-free login experience. You only need to set up SSH keys once, and after that, authentication is automatic.
Step 1: Generate an SSH Key
- Open your terminal and use the following command to generate a new SSH key:
ssh-keygen -t ed25519 -C "[email protected]"
If you’re using an older system that doesn’t support Ed25519, use rsa
:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
- When prompted, choose a location to save the key or press Enter to accept the default path (
~/.ssh/id_ed25519
). - Optionally, add a passphrase for extra security.
Step 2: Add Your SSH Key to the SSH Agent
To ensure seamless authentication, add your SSH key to the SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Step 3: Add the SSH Key to Your GitHub Account
- Copy your SSH key to your clipboard:
cat ~/.ssh/id_ed25519.pub
Copy the output.
- Log in to GitHub and navigate to Settings.
- In the SSH and GPG keys section, click New SSH key.
- Add a title for the key (e.g., “Laptop SSH Key”) and paste the key in the Key field.
- Click Add SSH key to save.
Step 4: Test the SSH Connection to GitHub
Verify the SSH connection with this command:
ssh -T [email protected]
If successful, you’ll see a message saying, “Hi username! You’ve successfully authenticated,” confirming the SSH setup is complete.
Step 5: Clone, Pull, and Push Using SSH
Once authenticated, you can perform Git operations without entering credentials each time. Ensure you use the SSH URL when cloning:
git clone [email protected]:username/repository.git
For pushing and pulling, simply use:
git push origin main
5. Switching Between HTTPS and SSH
If you ever need to switch between HTTPS and SSH, update the remote URL with:
git remote set-url origin <new_url>
For example, to switch to SSH:
git remote set-url origin [email protected]:username/repository.git
And to switch back to HTTPS:
git remote set-url origin https://github.com/username/repository.git
6. Troubleshooting Common Issues
- Permission denied (publickey): Ensure your SSH key is added to GitHub and to the SSH agent.
- Incorrect token scope: Verify that your PAT has the necessary scopes for actions like pushing or pulling.
- Expired or revoked PAT: Generate a new PAT if the old one has expired.
Final Thoughts
Logging into GitHub from the terminal with HTTPS or SSH provides a secure, efficient way to manage repositories. For occasional use, HTTPS with a PAT is quick and easy, while SSH is ideal for frequent workflows. With GitHub authentication set up in your terminal, you’re ready to leverage Git’s full potential and simplify your development workflow.