Skip to content
$ lynuxpro
← Git

LESSON 21 OF 21 · EXPERT · 20 MIN

Signed commits and repository security

HOW IT WORKSProving who made a commit

DeveloperSSH or GPG keySigned commitgit commit -SGitLab / GitHubhas public keyVerifiedbadgeUnverifiedrejected by ruleProtected mainsigned only
1/5

Anyone can set user.name to anything. A signature proves the commit came from a key you control.

Your Git repositories hold the source of everything you deploy, so they are a prime target. An attacker who can push a commit to main can ship code to production through your own pipeline. This final lesson covers how to prove who made a commit with signatures, how to protect branches on GitLab and GitHub, how to keep secrets out of repositories, and the habits that make a repository part of a secure software supply chain.

In simple words

  • Signed commit: a commit with a cryptographic signature that proves who made it. Author names alone can be faked.
  • Verified badge: GitLab or GitHub showing that a commit's signature matches a known key.
  • Secret scanning: tools that catch passwords and keys before or after they are committed.

Why author names prove nothing

The author of a commit is just text from git config. Anyone can commit as anyone:

mkdir trust-labcd trust-labgit initgit config user.name "Linus Torvalds"git config user.email "torvalds@linux-foundation.org"printf 'innocent change\n' > README.mdgit add README.mdgit commit -m "Totally legitimate commit"git log -n 1
SHELLbashutf-8 · 9L
[main (root-commit) 5b8c1d2] Totally legitimate commit 1 file changed, 1 insertion(+) create mode 100644 README.mdcommit 5b8c1d2…Author: Linus Torvalds <torvalds@linux-foundation.org>…
OUTPUTterminalutf-8 · 6L

Nothing stopped that. A cryptographic signature proves a commit was made by someone holding a specific private key.

Signing commits with an SSH key

Git can sign with GPG, SSH or X.509 keys. SSH signing (Git 2.34+) is the simplest, because you probably already have an SSH key and GitLab and GitHub both support it.

git config user.name "Leela Kumar"git config user.email "leela@acme.dev"ssh-keygen -t ed25519 -C "leela@acme.dev" -f ~/.ssh/id_ed25519_signinggit config --global gpg.format sshgit config --global user.signingkey ~/.ssh/id_ed25519_signing.pubgit config --global commit.gpgsign truegit config --global tag.gpgsign true
SHELLbashutf-8 · 7L
Generating public/private ed25519 key pair.Your identification has been saved in /home/lynuxpro/.ssh/id_ed25519_signingYour public key has been saved in /home/lynuxpro/.ssh/id_ed25519_signing.pubThe key fingerprint is:SHA256:qH3nS5d1fV0o9bK2mT7wX4pL8cR6yE1uA3sZ9vB0jN4 leela@acme.dev
OUTPUTterminalutf-8 · 5L
  • gpg.format ssh tells Git to sign with SSH keys (the setting keeps its historical name).
  • user.signingkey points to the public key; Git finds the private key next to it (or in your SSH agent).
  • commit.gpgsign true signs every commit automatically; -S signs one commit when the setting is off.

Then add the public key to GitLab (Preferences → SSH Keys, usage type "Signing" or "Authentication & Signing") or GitHub (Settings → SSH and GPG keys → New signing key).

Make a signed commit and verify it:

printf 'signed change\n' >> README.mdgit commit -am "Sign this change"git verify-commit HEADgit log --show-signature -n 1
SHELLbashutf-8 · 4L
[main 9e7f4a3] Sign this change 1 file changed, 1 insertion(+)Good "git" signature for leela@acme.dev with ED25519 key SHA256:qH3nS5d1fV0o9bK2mT7wX4pL8cR6yE1uA3sZ9vB0jN4commit 9e7f4a3…Good "git" signature for leela@acme.dev with ED25519 key SHA256:qH3nS5d1fV0o9bK2mT7wX4pL8cR6yE1uA3sZ9vB0jN4Author: Leela Kumar <leela@acme.dev>…
OUTPUTterminalutf-8 · 7L

To verify locally on your own machine, Git needs a list of trusted keys: git config gpg.ssh.allowedSignersFile ~/.ssh/allowed_signers, with lines like leela@acme.dev ssh-ed25519 AAAA…. On GitLab and GitHub, signed commits show a Verified badge when the key belongs to the committer's account.

Sign release tags too: git tag -s v1.2.0 -m "Release 1.2.0" and verify with git tag -v v1.2.0.

Protecting branches

Signatures prove identity; branch protection controls what can reach main. On GitLab (Settings → Repository → Protected branches, and Merge request settings) or GitHub (rulesets / branch protection), set:

  • No direct pushes to main and release branches; changes only through merge requests.
  • No force pushes and no branch deletion.
  • Required approvals, with code owners for sensitive paths (CODEOWNERS).
  • Pipelines must succeed before merging, including tests and security scans.
  • Signed commits required (GitLab push rule "Reject unsigned commits", GitHub "Require signed commits").
  • Branch must be up to date with the target before merging.
  • Protected tags (v*) so only maintainers can create releases.

GitLab push rules add server-side checks for everyone, which no --no-verify can skip: reject commits whose author email is not verified, reject files matching patterns like *.pem, enforce a commit message regex, and reject known secret files.

Keeping secrets out

Secrets in Git are the most common repository security incident. Defences in depth:

  1. Never put secrets in code or config files. Use environment variables injected by CI, a secrets manager (Vault, AWS Secrets Manager, Azure Key Vault), or Kubernetes Secrets managed by External Secrets or Sealed Secrets.
  2. .gitignore .env, key files and *.tfvars with real values; commit .example templates instead.
  3. Pre-commit scanning with gitleaks or detect-secrets, so secrets are blocked on the laptop.
  4. CI scanning (GitLab Secret Detection, GitHub secret scanning and push protection) as the server-side backstop.
  5. If a secret leaks: rotate it immediately, then remove it from history with git filter-repo and force-push, and check access logs for misuse.

Other supply-chain habits

  • Use SSH keys or short-lived tokens with minimal scopes for Git access; prefer deploy keys and CI job tokens over personal tokens in automation.
  • Enable two-factor authentication for every Git hosting account, and SSO where available.
  • Pin dependencies and commit lock files (package-lock.json, poetry.lock, .terraform.lock.hcl) so builds are reproducible and changes reviewed.
  • Review CI configuration changes carefully: .gitlab-ci.yml is code that runs with access to your secrets. Protect it with CODEOWNERS.
  • Sign and verify build artifacts (for example container images with Sigstore cosign) and generate SBOMs, so what runs in production can be traced back to a reviewed, signed commit.
  • Audit access regularly: remove people who left, and review who can push to protected branches.

Real-world scenario: the compromised laptop

A developer's laptop was compromised and their personal access token stolen. The attacker pushed a branch adding a cryptocurrency miner to the Dockerfile and opened a merge request. It went nowhere: main was protected, the merge request needed approval from a code owner of Dockerfile, the commits were unsigned while the project required signed commits, and the pipeline's container scan flagged the new binary. The security team revoked the token, rotated keys, and the only damage was a closed merge request. Each control alone could have failed; together they held.

Common mistakes

  • Trusting the author field as proof of identity.
  • Signing with a key that is not registered on GitLab or GitHub, so commits show "Unverified".
  • Protecting main but not release branches and tags.
  • Admins bypassing protections routinely, which makes them meaningless.
  • Personal access tokens with full scope stored in CI variables or scripts.
  • Deleting a leaked secret from the latest commit and thinking the problem is solved.

Practice

  1. Make a commit with a fake author name and see that Git accepts it.
  2. Configure SSH commit signing with a new key, make a signed commit and run git verify-commit HEAD.
  3. View signatures with git log --show-signature.
  4. Write down the branch protection settings and push rules you would enable for a production repository.
  5. Add gitleaks to a .pre-commit-config.yaml and describe where else it should run.

Cheat sheet

ssh-keygen -t ed25519 -C you@example.com -f ~/.ssh/id_ed25519_signinggit config --global gpg.format sshgit config --global user.signingkey ~/.ssh/id_ed25519_signing.pubgit config --global commit.gpgsign truegit commit -S -m "msg"               sign one commitgit verify-commit HEADgit log --show-signaturegit tag -s v1.2.0 -m "msg"  /  git tag -v v1.2.0gpg.ssh.allowedSignersFile           trusted keys for local verificationProtected branches · required approvals · CODEOWNERS · pipelines must passPush rules: reject unsigned commits, secret files, bad messages
OUTPUTterminalutf-8 · 11L

Key takeaways

  • Commit authors are unverified text; signatures prove which key made a commit.
  • SSH signing is the easiest way to sign: set gpg.format ssh, a signing key and commit.gpgsign true, and register the key on your Git host.
  • Protect main, release branches and tags: no direct or force pushes, required reviews, passing pipelines and signed commits.
  • Keep secrets out with ignores, pre-commit and CI scanning; rotate anything that leaks.
  • Repository security is a chain: identity, protection, review, scanning and signed artifacts together.