After going over dozens of SO posts, blogs, etc, I tried out every method, and this is what I came up with. It covers EVERYTHING.
These are all the ways and tools by which you can securely authenticate git to clone a repository without an interactive password prompt.
- SSH Public Keys
- API Access Tokens
- GIT_ASKPASS
- .gitconfig insteadOf
- .gitconfig [credential]
- .git-credentials
- .netrc
- Bonus: Works with Private Packages
- node / npm package.json
- python / pip / eggs requirements.txt
- ruby gems Gemfile
- golang go.mod
Best options for no plaintext storage
From what's asked here either SSH Keys, GIT_ASKPASS
, or git credential store
using the OS Keychain manager might be the best choice.
Since GIT_ASKPASS is probably the least understood of the 3, I'll detail that here - and the others are in the cheatsheet.
GIT_ASKPASS
How to create an GIT_ASKPASS
script:
echo 'echo $MY_GIT_TOKEN' > $HOME/.git-askpass
How to use it:
export MY_GIT_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export GIT_ASKPASS=$HOME/.git-askpass
git clone https://token@code.example.com/project.git
The script receives stdin in the form of:
Password for 'scheme://host.tld':
The script receives Git ENVs such as:
GIT_DIR=/Users/me/project/.git
GIT_EXEC_PATH=/usr/local/Cellar/git/2.19.0_1/libexec/git-core
GIT_PREFIX=
More details in the cheatsheet.