How to secure Git repositories and commits
· Category: Git
Short answer
Secure Git by signing commits with GPG, restricting access, scanning for secrets, and using secure authentication methods.
Steps
- Sign commits with GPG:
git commit -S -m "Signed commit"
- Enable branch protection rules on your platform.
- Use SSH keys instead of passwords.
- Scan for secrets with tools like GitLeaks or TruffleHog.
- Rotate credentials if a secret is accidentally committed.
Tips
- Require signed commits for critical branches.
- Use personal access tokens with minimal scope instead of passwords.
- Regularly audit repository access and collaborators.
Common issues
- Accidentally committed secrets: revoke them immediately; history rewriting is not enough.
- Unsigned commits in protected branches: update branch protection rules.