git-crypt lets you transparently encrypt files in a Git repository. Encrypted files are decrypted automatically when checked out, and encrypted when committed β so you can keep secrets (API keys, credentials, configs) right alongside your code without exposing them.
Install
| |
Initialize git-crypt in a Repository
| |
This generates a symmetric encryption key stored inside .git/git-crypt/.
Configure Which Files to Encrypt
Create (or edit) a .gitattributes file at the root of your repo to specify which paths should be encrypted:
| |
secrets/*β encrypts everything under thesecrets/directory.*.secretβ encrypts any file ending with.secret.
Commit the attributes file:
| |
From now on, any file matching these patterns will be automatically encrypted when pushed and automatically decrypted when pulled (as long as git-crypt is unlocked).
Add Sensitive Files
| |
On GitHub / GitLab the file will appear as binary gibberish. Locally it’s plain text β seamless.
Export the Key
You need the key to unlock the repo on another machine or share with teammates:
| |
β οΈ Keep this key safe! Anyone with the key can decrypt your secrets. Consider storing it in a password manager like Bitwarden.
Unlock a Cloned Repository
On a new machine, clone the repo then unlock with the exported key:
| |
All encrypted files are now readable in plain text.
Lock the Repository
When you’re done working and want to re-encrypt files on disk:
| |
This turns the encrypted files back into unreadable binary locally. Useful on shared machines or before handing off a laptop.
Verify Encryption Status
Check which files are being encrypted:
| |
Example output:
| |
Cheat Sheet
| Task | Command |
|---|---|
| Initialize | git-crypt init |
| Export key | git-crypt export-key <keyfile> |
| Unlock repo | git-crypt unlock <keyfile> |
| Lock repo | git-crypt lock |
| Check status | git-crypt status |
Tips
- Don’t commit the key file β add
git-crypt-keyto.gitignore. - One key per repo β each
git-crypt initcreates a unique key. - GPG users β you can also add collaborators via GPG keys with
git-crypt add-gpg-user <GPG-KEY-ID>, avoiding the need to share a symmetric key file. - CI/CD β base64-encode the key and store it as a CI secret, then decode and unlock during pipeline runs.
That’s it β git-crypt is one of the simplest ways to keep secrets in Git without leaking them. π