惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
小众软件
小众软件
爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - Franky
美团技术团队
博客园 - 【当耐特】
The Cloudflare Blog
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
IT之家
IT之家
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
WordPress大学
WordPress大学
V
Visual Studio Blog
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
有赞技术团队
有赞技术团队

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How to Set Up SSH & GPG Signed Commits on GitHub to Block...
Ankush Lokhande · 2026-06-01 · via DEV Community

👋 Hey all,
Welcome back to our blog!

Here's a scary thought:

Someone force-pushes to main.
Git history gets rewritten.
A malicious commit appears in the branch.

Nobody notices that the commit wasn't created by the developer it claims to be from.

This isn't just a theoretical risk. Misconfigured repositories, compromised accounts, and unverified commits can make it difficult to trust the history of your codebase.

Today, we're setting up two important layers of Git security that many developers overlook: SSH authentication and GPG commit signing.

Together, they help ensure:

✅ Only authorized machines can authenticate with GitHub using your SSH key
✅ Every commit is cryptographically signed and can be verified
✅ Rewritten history and suspicious commits become much easier to detect
✅ GitHub displays a Verified badge for authentic signed commits

Let's Begin

In this guide, we'll walk through the complete setup on macOS — from installing GitHub CLI and configuring SSH access to generating a GPG key and signing every commit you create.

Let's lock down our Git workflow. 🔐

Prerequisites

  • Mac with Homebrew installed
  • GitHub account
  • Terminal access

Setup Prerequisites

Configure SSH Authentication & GPG Signed Commits on GitHub

SSH authentication secures access to your GitHub repositories, while GPG signing verifies the authenticity of every commit you create. Together, they help protect your codebase and build trust in your Git history.

Start Coding

Step 1 — Install GitHub CLI

Run command to download gh:

brew install gh

gh is GitHub's official command-line tool. It allows you to authenticate with GitHub, generate SSH keys, and manage repositories directly from your terminal.

Step 2 — SSH Setup

Run:

gh auth login

Answer the following:

? Where do you use GitHub?
  GitHub.com
? What is your preferred protocol for Git operations on this host?
  SSH
? Generate a new SSH key to add to your GitHub account?
  Yes
? Enter a passphrase for your new SSH key (Optional)
  ********
? Title for your SSH key
  title_for_ssh_key // eg. organization_name
? How would you like to authenticate GitHub CLI?
  Login with a web browser

GitHub CLI will then display a one-time code:

! First copy your one-time code: XXXX-XXXX
Press Enter to open https://github.com/login/device in your browser...

Press Enter, open the provided URL, sign in to your GitHub account, and enter the one-time code when prompted.

After successful authentication, you'll see output similar to:

✓ Authentication complete.
✓ Configured git protocol
✓ Uploaded the SSH key to your GitHub account
✓ Logged in as your-username

This step automatically generates a new SSH key, uploads it to your GitHub account, and configures Git to use SSH for repository operations.

Step 3 — Install GnuPG

Run the following command:

brew install gnupg

Verify the installation:

gpg --version

GnuPG (GPG) is used to generate cryptographic keys that digitally sign your commits. GitHub uses these signatures to verify that a commit was created by you and has not been modified since it was signed.

Step 4 — Generate a GPG Key

Run:

gpg --full-generate-key

Select the following options:

Please select what kind of key you want:
(1) RSA and RSA

RSA keys may be between 1024 and 4096 bits long.
What keysize do you want?
4096

Please specify how long the key should be valid.
0 = key does not expire

Real name:
Your Name

Email address:
your-email@example.com

Review the information and confirm the key creation.

You'll then be prompted to create a passphrase for your key.

Choose a strong passphrase and store it securely. This passphrase protects your private GPG key and will be required when signing commits.

Step 5 — Find Your GPG Key ID

Run:

gpg --list-secret-keys --keyid-format LONG

Example output:

sec   rsa4096/ABC123DEF4567890 2026-06-01 [SC]
      1234567890ABCDEF1234567890ABCDEF12345678
uid                 [ultimate] Your Name <your-email@example.com>

Copy the value after rsa4096/:

ABC123DEF4567890

This is your GPG Key ID. You'll use it to export your public key and configure Git commit signing.

Step 6 — Export Your Public GPG Key

Replace <KEY_ID> with the key ID copied in the previous step:

gpg --armor --export <KEY_ID>

Example:

gpg --armor --export ABC123DEF4567890

You'll see output similar to:

-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----

Copy the entire block, including the BEGIN and END lines.

This is your public GPG key. GitHub uses it to verify commits signed with your private key.

Step 7 — Add Your GPG Key to GitHub

Open GitHub and navigate to:

Profile Picture → Settings → SSH and GPG Keys

Click:

New GPG Key

Paste the public key copied in the previous step:

-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----

Then click Add GPG Key.

Once added, GitHub can verify commits signed with your private GPG key and display the Verified badge on your commits.

Step 8 — Configure Git to Sign Commits

Replace <KEY_ID> with your GPG Key ID and run:

git config --global user.signingkey <KEY_ID>
git config --global commit.gpgsign true
git config --global gpg.program gpg

Example:

git config --global user.signingkey ABC123DEF4567890
git config --global commit.gpgsign true
git config --global gpg.program gpg

These settings tell Git which GPG key to use and automatically sign every commit you create.

Step 9 — Configure GPG Terminal Support

Run:

echo 'export GPG_TTY=$(tty)' >> ~/.zshrc
source ~/.zshrc

This allows GPG to interact correctly with your terminal when requesting your passphrase during commit signing.

Step 10 — Verify Commit Signing

Create a signed commit:

git commit -S -m "Test signed commit"

If prompted, enter the passphrase you created when generating your GPG key.

Push the commit to GitHub:

git push

Open the commit on GitHub. You should see a Verified badge next to the commit.

Verified Commit Example

The Verified badge confirms that GitHub successfully validated the GPG signature attached to your commit.

Step 11 — Protect Your Main Branch

For maximum security, enable branch protection rules on your repository.

Navigate to:

Repository → Settings → Branches

Edit your branch protection rule and enable:

✓ Require signed commits
✓ Block force pushes
✓ Require pull request reviews
✓ Require status checks before merging

These protections help ensure that only verified, reviewed, and trusted code can be merged into your main branch.


Let's Wrap!

SSH authentication protects repository access, while GPG signing verifies commit authenticity. Together, they provide a stronger security foundation for your Git workflow and make suspicious or rewritten commits much easier to identify. 🔐

One correction: GitHub doesn't literally have a setting called "Block force pushes" everywhere. Depending on the repository type, you'll typically see "Allow force pushes" (leave disabled) or "Restrict who can force push". So for accuracy, you may want to write:

✓ Require signed commits
✓ Disable force pushes
✓ Require pull request reviews
✓ Require status checks before merging

which is easier for readers to understand.

Thanks for reading

If you found this blog helpful or have any further questions, we would love to hear from you. Feel free to reach out and follow us on our social media platforms for more tips and tutorials on tech-oriented posts.

Happy coding!👨‍💻