Assumptions

  1. git is installed, and avaiable on PATH
  2. git-crypt is installed, and avaiable on PATH
  3. Repository of interest is at ~/repo
  4. Valid GPG key. See Creating Importing and Exporting GPG Keys to create/import one

Steps

  1. Configure your Git repository
cd ~/repo
git-crypt init
  1. Specify files to encrypt using .gitattributes file:
# Encrypt all files with name secretfile
secretfile filter=git-crypt diff=git-crypt
# Encrypt all files matching *.key, e.g., private.key
*.key filter=git-crypt diff=git-crypt
# Encrypt all files under folder secretdir recuvely
secretdir/** filter=git-crypt diff=git-crypt
# Don't encrypt matching files, e.g., .env-backup-example
*env*-example* !filter !diff  
  1. Add your GPG key to your repo. Replace $USER_ID with your GPG id, e.g., E1555F86885827379EC58C595EFCCCB6B24FD9FA
git-crypt add-gpg-user $USER_ID
  1. Add and commit your files. It will be automatically encrypted with git-crypt

You may lock and unlock the encrypted files with git-crypt lock and git-crypt unlock, respectively.