Learn how to authenticate to GitHub using different methods such as Personal Access Tokens and SSH keys for secure and efficient interactions with your GitHub repositories.
GitHub offers several authentication methods to ensure secure and seamless interactions with repositories. This tutorial covers using Personal Access Tokens (PATs) and SSH keys, both of which are recommended over the traditional username and password method, especially for private repositories and automation.
By the end of this tutorial, you will:
- Understand how to create and use Personal Access Tokens for GitHub authentication.
- Learn how to generate and set up SSH keys for GitHub authentication.
- Know how to configure Git to use a credential helper for easier authentication.
To follow this tutorial, you should:
- Have a GitHub account.
- Git installed on your system.
- Familiarity with the command line.
-
Navigate to GitHub Account Settings
- Open your GitHub account settings.
-
Developer Settings
- Click on "Developer settings" and then "Personal access tokens."
-
Generate New Token
- Click on "Generate new token" and select the necessary permissions (at least
repofor private repositories).
- Click on "Generate new token" and select the necessary permissions (at least
-
Copy and Secure Your Token
- Copy the generated token and store it in a secure location. Note: It won’t be shown again after this step.
- When prompted for a username and password while pushing to GitHub, use your GitHub username as the username and the Personal Access Token as the password.
-
Open Terminal
- Launch your terminal application.
-
Generate SSH Key
- Run
ssh-keygenand follow the prompts. - Save the SSH key to the default location (
~/.ssh/id_rsa). - Optionally, set a passphrase for added security.
- Run
-
Start SSH-Agent
- Run
eval "$(ssh-agent -s)"to start the SSH agent.
- Run
-
Add SSH Key
- Add your SSH private key to the agent by running:
ssh-add ~/.ssh/id_rsa.
- Add your SSH private key to the agent by running:
-
Copy SSH Key
- On macOS, run
cat ~/.ssh/id_rsa.pub | pbcopyto copy the key to your clipboard. On Linux, usexclip -selection clipboard < ~/.ssh/id_rsa.pub.
- On macOS, run
-
Add Key to GitHub
- Navigate to GitHub Settings → SSH and GPG keys → New SSH key.
- Paste the SSH key and save.
-
Change Repository’s Remote URL
-
Update your repository's remote URL to use SSH with the following command:
git remote set-url origin git@github.com:username/repository.git
Replace
usernameandrepositorywith your own details. -
- Enable Credential Helper
-
Run the following command to enable Git’s credential helper:
git config --global credential.helper cache
-
-
Set Custom Timeout
-
To increase the timeout duration (default is 15 minutes), use:
git config --global credential.helper 'cache --timeout=3600'
This sets the timeout to one hour.
-
- Credentials will be stored temporarily in memory for the duration of the cache timeout, making future pushes and pulls smoother without needing to re-enter them.
-
Store Credentials Permanently
-
If you'd prefer to store credentials indefinitely (not recommended for shared machines), use:
git config --global credential.helper store
This stores credentials in a plaintext file on your system.
-
Here’s a common use case for setting up GitHub authentication:
-
Create a Personal Access Token as described in the tutorial.
-
Clone a Repository using the token for authentication:
git clone https://github.com/username/repository.git
-
Push Changes to GitHub using the token as the password:
git push origin main
- Pro Tip: SSH keys are more secure than using a Personal Access Token in some cases, especially when automating tasks.
- Warning: Using the credential store method for permanent storage on shared machines may expose your credentials.
Your contributions can make this tutorial even better:
-
Fork the repository.
-
Create a new branch:
git checkout -b my-awesome-feature
-
Make your invaluable changes.
-
Commit your changes:
git commit -am 'Added some awesome features' -
Push to the branch:
git push origin my-awesome-feature
-
Create a new Pull Request targeting the Notes directory.
Contributions are welcome! Feel free to open issues, suggest enhancements, or submit pull requests to improve the tutorial.
- Raphael Chookagian | GitHub Profile
- 12/10/2024
-
This tutorial is provided as-is without any warranties. Users are advised to review and understand the content before executing any commands.
-
This project is licensed under the MIT License. See the LICENSE file for details.
