Can I hold git credentials in environment variables?

前端 未结 3 794
死守一世寂寞
死守一世寂寞 2020-12-13 19:25

I\'d like to create a very simple shell script, which will ultimately be called by another application, that updates a local git repository:

#!/bin/bash

cd          


        
相关标签:
3条回答
  • 2020-12-13 19:51

    Yes you can pass you git Passwort/Username with env, using git-credential-env

    GIT_USER=foo
    GIT_PASS=bar
    npm install -g git-credential-env
    git config credential.helper "env --username=GIT_USER --password=GIT_PASS"
    

    The wiki in this project says

    You can accomplish the same behavior by using an inline bash function:
    GIT_USER=foo
    GIT_PASS=bar
    git config credential.helper "!f() { echo \"username=${GIT_USER}\\npassword=${GIT_PASS}\"; }; f"
    
    0 讨论(0)
  • 2020-12-13 19:53

    I know that it's very old question but if you really need to pass username and password for HTTP basic authentication you can just set helper like this:

    git config credential.helper '!f() { sleep 1; echo "username=${GIT_USER}"; echo "password=${GIT_PASSWORD}"; }; f'
    

    UPDATE: I've added sleep 1 to the function. In some environments it may be probably needed due to race condition. I've got 2 virtual machines running Debian Jessie. They had the same architecture but different CPU and different number of cores. On one of these machines the helper was working fine without sleep. On the other one it wasn't. After few hours of debugging I run strace to see what's happening. And it magically started to work. strace just made git a little bit slower.

    0 讨论(0)
  • 2020-12-13 20:02

    You can set the username in the git config with:

    git config credential.https://github.com.username $GIT_USER
    

    Then you can set the GIT_ASKPASS environment variable to a script that will provide the password:

    export GIT_ASKPASS=/path/to/git_env_password.sh
    

    The contents of git_env_password.sh would be:

    #!/bin/bash
    echo $GIT_PASSWORD
    

    N.B: This will store the username in the git config, so if you are not okay with that use another solution.

    For more info consult the gitcredentials man page.

    0 讨论(0)
提交回复
热议问题