Git
How to Log In to GitHub from Terminal?
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:
- Seamless Integration: Enables direct communication between your local repositories and GitHub.
- Enhanced Productivity: Simplifies workflows for cloning, pulling, and pushing code.
- 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
- Log in to your GitHub account.
- Navigate to Settings > Developer Settings > Personal Access Tokens.
- Click Generate new token and select appropriate scopes (e.g.,
repo
for repository access). - 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
):
- Use your GitHub username as the username.
- 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
- Open your terminal and run:
ssh-keygen -t ed25519 -C "[email protected]"
Replace [email protected]
with your GitHub email address.
- Follow the prompts to save the key pair. By default, keys are stored in
~/.ssh/
. - 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
- Copy the public key to your clipboard:
cat ~/.ssh/id_ed25519.pub
- Log in to GitHub and navigate to Settings > SSH and GPG Keys.
- 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.