strboul blog

Synchronize pass with git


I like pass, "the standard Unix password manager", that is a reliable and secure solution for password management. It is a great option for those who value security and control over their password management, it is essentially a simple script that uses GnuPG for encryption, and other utility tools such as git, tree, xclip, qrcode; but it may not be suitable for those who are not comfortable with technical setup and maintenance. It requires some technical know-how to use it properly. You need to set up the right command-line interface, a GnuPG key, and a backup process.

One of the most popular backup alternative is git. Using pass in conjunction with git offers several benefits, including the ability to synchronize your passwords across multiple devices, logging, historical tracking, and conflict resolution.

Example

In this snippet, we initialize pass and git repository, and create a post-commit hook that automatically pushes to the origin when there is a change in the repo.

pass init <fingerprint> # add multiple keys if needed.

pass git init
# Initialized empty Git repository in ~/.password-store/.git/

pass git remote add origin git@<service>.com:<user>/<repo>.git

pass git push --set-upstream origin master

filepath_post_commit="$(pass git rev-parse --show-toplevel)/.git/hooks/post-commit"
cat << EOF > "$filepath_post_commit"
#!/bin/sh
git push origin master:master
EOF
chmod +x "$filepath_post_commit"

Then we test it.

pass insert foo/bar
# Enter password for foo/bar: ***
# Retype password for foo/bar: ***
# ... (git push)

pass ls
# Password Store
# └── foo
#     └── bar

pass edit foo/bar
# ... (edit in the editor)
# ... (git push)

Optionally, it's a good idea to create a shell alias to override the behavior of pass that does a git pull every time you command. It is a way of being sure that you are always in sync with the git repository, especially important when multiple people/devices use it.

alias pass="pass git pull && pass"

It is a kind of on-demand solution for syncing. Alternatively, you can hook up git pull to your system daemon to periodically update it with cron or systemd-timer.

Caveats and alternatives