7

I would like to set the value of github.token in my ~/.gitconfig to be the result of a shell command. I currently have the following:

[github]
  user = zmanji
  token = !echo ~/.githubtoken 2> /dev/null

However git config github.token does not return the contents of the ~/.githubtoken file but the command itself. How can I get this to work as desired?

Edit: Just to be clear, I am trying to achieve what is implied here:

You can also define github.token to be a command which returns the actual token on stdout by setting the variable to a command string prefixed with !.

Zameer Manji
  • 3,017
  • 5
  • 31
  • 42

3 Answers3

3

Instead of storing my GitHub token in a file, I store it in my OS X Keychain and get it like this (snippet from my .gitconfig):

[github]
  token = !security find-generic-password -gs \"GitHub API Token\" 2>&1 >/dev/null | awk '/password/ {print $2}' | tr -d \\\"
Andy Stewart
  • 5,013
  • 2
  • 28
  • 38
2

It appears the catch here is that he is not setting the token in the gitconfig setting. He is using defunkt's hub tool. This is a wrapper for the git command which among other things, allows you to have GITHUB_USER and GITHUB_TOKEN environment variables. Which will override the settings in the local .gitconfig file.

Then to make it seamless the user you pointed to aliased alias git=hub in his ZSH config. You should be able to then source a local file where you set your environment variables and push your repository to the public world with all of your private information in tact.

**NOTE for homebrew users on OSX, you can install the tool via brew install hub.

Geoff Lanotte
  • 7,490
  • 1
  • 38
  • 50
  • I wasn't trying to use hub. I was trying to get git config to give me a value that I did not explicitly store in the file. – Zameer Manji Sep 06 '11 at 21:12
  • fair enough, it looked like you were just trying to store the value of the github token outside of the gitconfig so you could put it in a repo of its own (that was my intent when I found this). At any rate, best of luck! – Geoff Lanotte Sep 06 '11 at 21:19
  • That's exactly it. Thats why I wanted my gitconfig to give me a value that I did not store in it. That way I could share my entire gitconfig without giving away public information. I think using the environment variables as you suggest will be a better solution in the long term as I can have this functionality in my bash_profile or bashrc. – Zameer Manji Sep 06 '11 at 21:22
0

From what I can infer from the git config man page, only git config alias.* has the possibility to define shell commands.

So maybe defunkt was talking about an alias called token

git config alias.token '!security 2>&1 >/dev/null find-generic-password -gs github.token | ruby -e 'print $1 if STDIN.gets =~ /^password: \\\"(.*)\\\"$/''

It would be used for quickly getting back the value of his GitHub token.
The return value would be then assigned to github.token through a classic git config github.token xxx.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • My goal is to have git config github.token return my token without storing it in my .gitconfig. It seems other people are doing this as well: https://github.com/mattly/dotfiles/blob/master/gitconfig#L6 . – Zameer Manji Aug 31 '11 at 11:27
  • @Zameer and you did try `!sh -c 'echo $GITHUB_TOKEN'` instead of `!echo` directly? – VonC Aug 31 '11 at 11:46
  • Yes, the string "!sh -c 'echo $GITHUB_TOKEN'" is returned and not the value of the env variable. – Zameer Manji Aug 31 '11 at 13:38
  • @Zameer: exactly the result I got on my side. In other words, I am not sure it actually works. – VonC Aug 31 '11 at 13:48
  • Can anybody tell me what is `github.token`? I've heard `github.user` but what value does `github.token` have? – Santosh Kumar Sep 24 '12 at 18:38
  • @SantoshKumar As commented in http://stackoverflow.com/questions/7129232/problem-in-pushing-to-github/7130405#7130405, GitHub no longer use (for non-ssh communication, that is git:// or https:// protocols) plain token, but OAuth 2 token: https://github.com/blog/1270-easier-builds-and-deployments-using-git-over-https-and-oauth: so no need for a `github.token` in your config. – VonC Sep 24 '12 at 21:10