How to Secure Git Repositories: A Developer’s Checklist

To secure git repositories, treat the repository as a place where code, access, and automation meet. A leak rarely comes from one mistake. It comes from a secret left in an old commit, a branch anyone can push to, an unsigned commit nobody checked, a dependency nobody reviewed, or a CI token with more access than the job needs. This checklist walks through each of those spots with steps you can apply to a new project this week and to an older project over a few working sessions.

How to Secure Git Repositories Without Slowing the Team

Security steps fail when they block normal work. The way to keep them in place is to put the check where the work already happens. A pre-commit hook catches a secret before it leaves a laptop. A branch rule catches a direct push before it reaches main. A CI check catches a bad dependency before it ships. Each check sits on a path developers already use, so the secure path is also the easy path.

Roll it out in small steps: secret scanning first, then branch protection, then signed commits, then dependency review.

Git History Secrets: Find Them, Remove Them, Rotate Them

Secrets in history are the hardest item on this list, because deleting a file from the latest commit does not delete it from earlier commits. Anyone who clones the repository still gets the full history, including the commit where the secret first appeared. The fix has two parts, and both matter. Remove the secret from history, and rotate the secret so the old value stops working.

First, find what is exposed. Run a secret scanner across the full history to catch git history secrets, not just the current files, and scan test fixtures and example configs too, since keys often hide in files that look harmless. If you use a secrets manager, this is the point where it pays off. A comparison of Vault vs AWS Secrets Manager vs Doppler can help a team pick a store that fits its stack, but any manager beats a key pasted in a config file.

Next, rewrite history with a tool built for the job, such as git filter-repo, and force-push the cleaned history to the shared remote. Warn the team first, because a rewritten history requires every open branch to rebase. After the push, rotate every exposed credential, even if you believe nobody saw it. A secret that lived in a cloned copy is still a live secret until it is revoked. For teams that already share credentials across members, the Bitwarden vs 1Password comparison for engineering teams covers how shared vaults handle rotation and access changes without passing keys around in chat.

Prevent the next leak with a pre-commit hook that scans staged files, plus a CI scan on every pull request. The CI scan catches cases where someone skipped the hook or committed from a fresh machine.

Branch Protection Rules for Main Branches

Branch protection rules decide who can change the main branch and how. At a minimum, block direct pushes to main and to release branches, require a pull request, and require at least one review from someone other than the author. For a small team, one reviewer is enough to catch both mistakes and quiet shortcuts. For a larger team, require two reviewers on sensitive paths such as payment code, auth code, and infrastructure configs.

Add status checks to the same rule. Require the test suite and the secret scan to pass before merge, and require branches to be up to date with main before merging. That last option stops a pull request that passed tests against an old base from landing on top of conflicting changes. Also turn on rules for force pushes and branch deletion, so history on main cannot be rewritten or removed by accident or on purpose without an admin decision.

Review the rule itself once a quarter. Admins often hold a bypass for emergencies, and that bypass is fine as long as it is used rarely and logged. If the bypass becomes the normal path, the fix is usually a faster review process rather than a stricter rule.

Signed Git Commits and Verified Authors

Signed git commits prove that a commit came from the person named as its author, using a private key only that person holds. Without signing, anyone can set the author name and email on a commit to match a teammate, and the history will show that teammate as the author. Signing closes that gap, and hosting platforms show a verified badge when the signature matches a key on the account.

Set up signing with SSH keys or GPG keys, whichever your team already uses for code hosting, and configure Git to sign by default so developers do not have to remember a flag on every commit. Ask each developer to add their public key to their hosting account and to protect the private key with a passphrase.

Dependency Review Before Merge

Dependencies carry code you did not write, and that code runs with the same trust as your own. Review new dependencies when they are added, not at release time when removing one is painful. Check who maintains the package, when it was last updated, and whether it pulls in a long chain of smaller packages you never asked for.

Pin versions in lockfiles and commit the lockfile to the repository, so every developer and every CI run installs the same set. Turn on automated dependency alerts from your host, and set a regular slot, such as one morning a week, to work through them. Security updates for direct dependencies come first. Updates that only touch test tooling can wait for the next routine pass.

Keep a short note in the pull request when you add a dependency: what it does, why the project needs it, and what you checked. Over a year, those notes become the record of why each dependency earned its place.

Access Reviews for People and Tokens

Access review means checking, on a schedule, who can read and write each repository and whether they still need that access. People change teams and contractors finish work, yet former maintainers keep access by default unless someone removes it. A quarterly pass over members, teams, and outside collaborators catches most of these cases.

Apply the same review to tokens and deploy keys. List every personal access token, deploy key, and app installation tied to the repository, note what each one can do, and remove the ones nobody can explain. Prefer short-lived tokens and tokens scoped to a single repository over broad tokens that work everywhere. When a developer leaves a team, revoke their tokens in the same session as removing their account access, so a leftover token does not outlive the person it belonged to.

Server access follows the same habit. A repository often deploys to a server that was set up once and never reviewed. The steps to harden a fresh Ubuntu server cover the server side of that chain, including user accounts and SSH keys, which matter just as much as the repository settings when a deploy key can push code straight to production.

CI Secrets Handling in Pipelines

CI secrets handling decides how build jobs get the keys they need without printing them or passing them to the wrong job. Store secrets in the CI system or a secrets manager, never in the repository and never in a build script. Scope each secret to the environment that needs it, so a test job cannot read a production key, and a pull request from a fork cannot read any secret at all unless you explicitly allow it.

Turn on log masking if your CI tool offers it, and check a sample build log after setup to confirm a secret value does not appear in plain text. Keep build scripts free of commands that print the environment, since a debug line added to fix one failure can dump every secret into a log many people can read. A guide to how to build a CI/CD pipeline with GitHub Actions shows where secrets, environments, and approvals fit in a real pipeline, which helps when you map each secret to exactly one job.

Rotate CI secrets on the same schedule as other credentials, and rotate them at once after any suspected exposure, such as a log that leaked or a contributor who should not have had access. If a pipeline deploys with GitOps, the same discipline applies to the deployment repository. The overview of what GitOps with Argo CD means for deploys explains why the deployment repository deserves the same protection as the code repository, since a change there becomes a change in production.

Close the loop monthly: branch rules on, secret scanning on, member list current, no new broad token.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.