A git credential helper interfacing with pass, the standard unix password manager.
A git credential helper implementation that allows using pass as the credential backend for your git repositories. This is achieved by explicitly defining mappings between hosts and entries in the password store.
It is recommended to configure GPG to use a graphical pinentry program. That way, you can also use this helper when git is invoked via GUI programs such as your IDE. For a configuration example, refer to the ArchWiki. In case you really want to use the terminal for pinentry (via
pinentry-curses), be sure to appropriately configure the environment variable
GPG_TTY, most likely by adding the following lines to your shell initialization:
GPG_TTY=$(tty) export GPG_TTY
If you use this setup for remote work via SSH, also consider the alternative of GPG agent forwarding.
If possible, use an available package for your Linux distribution or operating system such as the ones linked below.
sudo pip install .
This might potentially install Python packages without the knowledge of your system's package manager. If all package preconditions are already met, you can also copy the script file to to your system to avoid this problem:
sudo cp passgithelper.py /usr/local/bin/pass-git-helper
Another option is to install the script in an isolated virtualenv:
virtualenv /your/env /your/env/pip install .
Create the file
~/.config/pass-git-helper/git-pass-mapping.ini. This file uses ini syntax to specify the mapping of hosts to entries in the passwordstore database. Section headers define patterns which are matched against the host part of a URL with a git repository. Matching supports wildcards (using the python fnmatch module). Each section needs to contain a
targetentry pointing to the entry in the password store with the password (and optionally username) to use.
To instruct git to use the helper, set the
credential.helperconfiguration option of git to
/full/path/to/pass-git-helper. In case you do not want to include a full path, a workaround using a shell fragment needs to be used, i.e.
!pass-git-helper [email protected]must be the option value. The option can be set using the CLI with:
git config credential.helper '!pass-git-helper [email protected]'
If you want to match entries not only based on the host, but also based on the path on a host, set
truein your git config, e.g. via:
git config credential.useHttpPath true
Afterwards, entries can be matched against
host.com/path/to/repoin the mapping. This means that in order to use a specific account for a certain Github project, you can then use the following mapping pattern:
Please note that when including the path in the mapping, the mapping expressions need to match against the whole path. As a consequence, in case you want to use the same account for all Github projects, you need to make sure that a wildcard covers the path of the URL, as shown here:
The host can be used as a variable to address a pass entry. This is especially helpful for wildcard matches:
The above configuration directive will lead to any host that did not match any previous section in the ini file to being looked up under the
git-loginsdirectory in your passwordstore.
includeIfdirective available in git >= 2.13, it is also possible to perform matching based on the current working directory by invoking
pass-git-helperwith a conditional
MAPPING-FILE. To achieve this, edit your
.gitconfig, e.g. like this:
[includeIf "gitdir:~/src/user1/"] path=~/.config/git/gitconfig_user1 [includeIf "gitdir:~/src/user2/"] path=~/.config/git/gitconfig_user2
With the following contents of
mapping_user1.ini, which could contain a
targetentry to e.g.
github.com/user1would always be invoked in
[user] name = user1 [credential] helper=/full/path/to/pass-git-helper -m /full/path/to/mapping_user1.ini
See also the offical documentation for
Defaults suitable for all entries of the mapping file can be specified in a special section of the configuration file named
[DEFAULT]. Everything configure in this section will automatically be available for all further entries in the file, but can be overriden there, too.
As usual with pass, this helper assumes that the password is contained in the first line of the passwordstore entry. Though uncommon, it is possible to strip a prefix from the data of the first line (such as
password:by specifying an amount of characters to leave out in the
skip_passwordfield for an entry or also in the
[DEFAULT]section to apply for all entries:
[DEFAULT] # length of "password: " skip_password=10
for some reasons, this entry doesn't have a password prefix
pass-git-helpercan also provide the username necessary for authenticating at a server. In contrast to the password, no clear convention exists how username information is stored in password entries. Therefore, multiple strategies to extract the username are implemented and can be selected globally for the whole passwordstore in the
[DEFAULT]section, or individually for certain entries using the
[DEFAULT] username_extractor=regex_search regex_username=^user: (.*)$
use a fixed line here instead of a regex search
The following strategies can be configured:
Extracts the data from a line indexed by its line number. Optionally a fixed-length prefix can be stripped before returning the line contents.
line_username: Line number containing the username, 0-based. Default: 1 (second line)
skip_username: Number of characters to skip at the beginning of the line, for instance to skip a
user:prefix. Similar to
skip_password. Default: 0.
Searches for the first line that matches a provided regular expressions and returns the contents of that line that are captured in a regular expression capture group.
regex_username: The regular expression to apply. Has to contain a single capture group for indicating the data to extract. Default:
Returns the last path fragment of the passwordstore entry as the username. For instance, if a regular pass call would be
pass show dev/github.com/languitar, the returned username would be
No configuration options.
By default, passwordstore entries are assumed to use UTF-8 encoding. If all or some of your entries use a different encoding, use the
encodingkey (for instance, in the
DEFAULTsection) to specify the used encoding.
-lcan be given as an option to the script to produce logging output on stderr. This might be useful to understand how the mapping is applied.
-m MAPPING_FILEcan be specified to use an alternative mapping file location.
In some automated contexts it might be necessary to prevent GPG from asking for the passphrase (via the agent). To achieve this, you can disable the complete processing of this helper by defining the environment variable
PASS_GIT_HELPER_SKIPwith any content (or no content at all). pass-git-helper will return immediately in this case, indicating to git that no suitable credentials could be found.
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version. This work is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.