Last Updated: February 25, 2016
·
3.134K
· clarete

Saving github auth info in Mac OS X keychain

I have basically two problems to solve:

1) I like use the gist and some other github command line tools and I don't want to type my password every time I use them.

2) I'm not comfortable saving my password in plain text files

The answer for the first problem is the use of the git config utility. And it's actually the cause of the second problem!

My answer for the second problem is the security tool that comes with Mac OS and allows you to access your passwords from the command line.

here is the github section in my .gitconfig file:

[github]
    user = clarete
    password = !security find-generic-password -wgs org.comum.lincoln.conf.github | tr -d '\n'

going deeper

  • You can prefix all the values in your git config variables with an exclamation mark (!) to make git execute the values as a shell command.

  • I had to use the | tr -d '\n' to get rid of an annoying new line that the security utility prints out.

  • I have a keyring entry called org.comum.lincoln.conf.github in my keychain. Maybe just github.password would be better, but I love namespaces.

7 Responses
Add your response

Thank you Lincoln!! I was looking for this and it works like a charm!!

over 1 year ago ·

Or, if you're already using keychain to save your browser password for github, you can reuse it here by substituting the following command:

security find-internet-password -wgs github.com
over 1 year ago ·

It's weird, for me it shows that -w is not a valid option... Any ideas?

over 1 year ago ·

@felipeelias it's weird, maybe your mac os version? Anyway, you can still call security without the '-w' flag and use a program to filter the output, like this:

security find-generic-password -gs org.comum.lincoln.conf.github 2>/dev/stdout 1>/dev/null | sed 's/^password: \"\(.*\)\"/\1/g' | tr -d '\n'

Ugly but seems to work :)

over 1 year ago ·

@clarete definitely my os version :-) thanks!

over 1 year ago ·

Password caching is already available (as of git 1.7.10) using the credential helper. It handles storing/retrieving your password from your OSX Keychain without having to do any of the sed or tr parsing you have posted here. You can learn how to set this up on the password-caching section of the GitHub help page at https://help.github.com/articles/set-up-git

Another option is to use the SSH URL instead of HTTPS.
https://help.github.com/articles/why-is-git-always-asking-for-my-password

If you set up SSH keys on your GitHub account, you won't have to bother with passwords at all.

Hope this helps

over 1 year ago ·

@insomniacsoftware Nice resource! But actually this password is not for git itself, but for other github tools, like gist. Do you know if these tools support this stuff?

over 1 year ago ·