A Beginner's Guide to `git-secret`: Keep Your Secrets Safe in Git
git-secret
is an open-source tool that:
It's perfect for storing things like:
First, you'll need to install git-secret
and its dependencies.
brew install git-secret gnupg
sudo apt-get install git-secret gnupg
choco install git-secret
Navigate to your Git repository and run:
git secret init
This creates a .gitsecret
directory that will store encryption keys and configuration.
Before adding secrets, you need to specify who can access them. Each team member needs a GPG key.
To add yourself:
gpg --gen-key # If you don't have a GPG key yet
gpg --list-keys # Find your key ID
git secret tell your@email.com
To add another team member, have them send you their public key, then:
gpg --import team-member.pub
git secret tell team@member.com
Let's say you have a file config.ini
with sensitive data that you want to protect:
git secret add config.ini
.gitsecret/paths/mapping.cfg
but not yet encrypted.To encrypt all files you've added:
git secret hide
This creates an encrypted version of each file with .secret
appended (e.g., config.ini.secret
).
Now you can safely commit the encrypted files:
git add config.ini.secret
git commit -m "Add encrypted config file"
Make sure to add *.secret
to your .gitignore
file to prevent accidentally committing unencrypted versions.
When you or a team member needs to access the secrets:
git secret reveal
This decrypts all files to their original names and locations.
.gitignore
to exclude the original files.git secret killperson old@member.com
.Add a pre-commit hook to ensure secrets are always encrypted before committing:
echo "git secret hide" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
To see which files are being tracked by git-secret:
git secret list
To stop tracking a file:
git secret remove config.ini
git-secret
provides a simple yet powerful way to protect sensitive data in your Git repositories. By following this guide, you can securely share configuration files and credentials with your team without exposing them in your version control history.
Remember that security is an ongoing process - regularly review who has access to your secrets and keep your encryption keys safe.
Happy (and secure) coding!