| title | GitHub Security | |||||
|---|---|---|---|---|---|---|
| description | Learn how to secure your GitHub account and repositories using SSH keys, HTTPS, personal access tokens, two-factor authentication, and best practices. | |||||
| tags |
|
|||||
| keywords |
|
|||||
| sidebar_position | 13 |
Welcome to the Git & GitHub Tutorial Series by CodeHarborHub. Securing your GitHub account and repositories is critical to protect your code, collaboration workflow, and sensitive data. In this guide, you’ll learn essential security practices used by professional developers.
When connecting your local repository to GitHub, you have two options:
git clone https://github.com/username/repo.git- Uses username and password / personal access token for authentication
- Easy setup, works behind firewalls
- Recommended for beginners or temporary setups
git clone git@github.com:username/repo.git- Uses SSH keys instead of passwords
- More secure and convenient for frequent access
- Recommended for professional developers
:::tip Modern GitHub disables password authentication for HTTPS; you need a **personal access token (PAT) :::
SSH keys allow secure password-less authentication between your machine and GitHub.
ssh-keygen -t ed25519 -C "your.email@example.com"- Press Enter to save in default location (
~/.ssh/id_ed25519) - Set a passphrase for extra security
- Copy public key:
cat ~/.ssh/id_ed25519.pub- Go to GitHub → Settings → SSH and GPG keys → New SSH key
- Paste the key and save
ssh -T git@github.comYou should see a welcome message confirming your key is working.
For HTTPS authentication or API access, GitHub uses personal access tokens.
- Go to GitHub → Settings → Developer Settings → Personal Access Tokens → Tokens (classic) → Generate new token
- Choose scopes/permissions depending on your need (e.g.,
repo,workflow,admin:repo_hook) - Copy the token (store securely — you won’t see it again!)
git clone https://github.com/username/repo.git
# Username: your GitHub username
# Password: paste your PAT:::info PATs replace your GitHub password for Git operations and API access. :::
2FA adds an extra layer of security to your GitHub account.
- Go to GitHub → Settings → Security → Two-factor authentication
- Choose Authenticator App or SMS
- Follow setup instructions and save recovery codes
:::tip Even if your password is compromised, attackers cannot access your account without the second factor. :::
- Prevent direct commits to
mainormaster - Require pull requests and code reviews before merging
- Assign roles carefully: Admin, Write, Read
- Avoid giving unnecessary write access
- GitHub Dependabot automatically scans for vulnerable dependencies
- Suggests updates for safe libraries
- Never commit sensitive files (
.env, API keys) - Store secrets in GitHub Actions secrets for CI/CD
- Check authorized OAuth apps
- Review SSH keys and tokens
- Remove inactive collaborators
| Feature | Purpose |
|---|---|
| HTTPS / SSH | Secure Git connections |
| SSH Keys | Password-less authentication |
| Personal Access Token | Secure HTTPS access and API usage |
| Two-Factor Authentication | Extra layer of account security |
| Protected Branches | Prevent unauthorized changes |
| Dependabot | Automatic dependency vulnerability alerts |
| Repository Secrets | Secure sensitive data for workflows |
With security set, you’re ready to explore GitHub Actions — learn how to automate workflows, CI/CD pipelines, and project automation. 👉 Next: GitHub Actions →
- GitHub Docs – SSH Keys
- GitHub Docs – Personal Access Tokens
- GitHub Docs – Two-Factor Authentication
- GitHub Security Best Practices
💙 This tutorial is part of the CodeHarborHub Git & GitHub series — helping developers secure their accounts, repositories, and workflows professionally.