Is there a way to cache GitHub credentials for pushing commits?

后端 未结 24 3258
囚心锁ツ
囚心锁ツ 2020-11-21 05:02

I recently switched to synchronizing my repositories to https:// on GitHub (due to firewall issues), and it asks for a password every time.

Is there a way to cache t

相关标签:
24条回答
  • With Git version 1.7.9 and later

    Since Git 1.7.9 (released in late January 2012), there is a neat mechanism in Git to avoid having to type your password all the time for HTTP / HTTPS, called credential helpers. (Thanks to dazonic for pointing out this new feature in the comments below.)

    With Git 1.7.9 or later, you can just use one of the following credential helpers:

    git config --global credential.helper cache
    

    ... which tells Git to keep your password cached in memory for (by default) 15 minutes. You can set a longer timeout with:

    git config --global credential.helper "cache --timeout=3600"
    

    (That example was suggested in the GitHub help page for Linux.) You can also store your credentials permanently if so desired, see the other answers below.

    GitHub's help also suggests that if you're on Mac OS X and used Homebrew to install Git, you can use the native Mac OS X keystore with:

    git config --global credential.helper osxkeychain
    

    For Windows, there is a helper called Git Credential Manager for Windows or wincred in msysgit.

    git config --global credential.helper wincred # obsolete
    

    With Git for Windows 2.7.3+ (March 2016):

    git config --global credential.helper manager
    

    For Linux, you would use (in 2011) gnome-keyring(or other keyring implementation such as KWallet).

    Nowadays (2020), that would be (on Linux)

    Fedora

    sudo dnf install git-credential-libsecret
    git config --global credential.helper /usr/libexec/git-core/git-credential-libsecret
    

    Ubuntu

    sudo apt-get install libsecret-1-0 libsecret-1-dev
    cd /usr/share/doc/git/contrib/credential/libsecret
    sudo make
    git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
    

    With Git versions before 1.7.9

    With versions of Git before 1.7.9, this more secure option is not available, and you'll need to change the URL that your origin remote uses to include the password in this fashion:

    https://you:password@github.com/you/example.git
    

    ... in other words with :password after the username and before the @.

    You can set a new URL for your origin remote with:

    git config remote.origin.url https://you:password@github.com/you/example.git
    

    Make sure that you use https, and you should be aware that if you do this, your GitHub password will be stored in plaintext in your .git directory, which is obviously undesirable.

    With any Git version (well, since version 0.99)

    An alternative approach is to put your username and password in your ~/.netrc file, although, as with keeping the password in the remote URL, this means that your password will be stored on the disk in plain text and is thus less secure and not recommended. However, if you want to take this approach, add the following line to your ~/.netrc:

    machine <hostname> login <username> password <password>
    

    ... replacing <hostname> with the server's hostname, and <username> and <password> with your username and password. Also remember to set restrictive file system permissions on that file:

    chmod 600 ~/.netrc
    

    Note that on Windows, this file should be called _netrc, and you may need to define the %HOME% environment variable - for more details see:

    • Git - How to use .netrc file on Windows to save user and password
    0 讨论(0)
  • 2020-11-21 05:03

    It wasn't immediately obvious to me that I needed to download the helper first! I found the credential.helper download at Atlassian's Permanently authenticating with Git repositories.

    Quote:

    Follow these steps if you want to use Git with credential caching on OS X:

    Download the binary git-credential-osxkeychain.

    Run the command below to ensure the binary is executable:

    chmod a+x git-credential-osxkeychain
    

    Put it in the directory /usr/local/bin.

    Run the command below:

    git config --global credential.helper osxkeychain
    
    0 讨论(0)
  • 2020-11-21 05:04

    You can just use

    git config credential.helper store
    

    When you enter password next time with pull or push, it will be stored in file .git-credentials as plain text (a bit unsecure, but just put it into a protected folder).

    And that's it, as stated on this page:

    git-credential-store

    0 讨论(0)
  • 2020-11-21 05:04

    An authentication token should be used instead of the account password. Go to GitHub settings/applications and then create a personal access token. The token can be used the same way a password is used.

    The token is intended to allow users not use the account password for project work. Only use the password when doing administration work, like creating new tokens or revoke old tokens.


    Instead of a token or password that grants a user whole access to a GitHub account, a project specific deployment key can be used to grant access to a single project repository. A Git project can be configured to use this different key in the following steps when you still can access other Git accounts or projects with your normal credential:

    1. Write an SSH configuration file that contains the Host, IdentityFile for the deployment key, maybe the UserKnownHostsFile, and maybe the User (though I think you don't need it).
    2. Write an SSH wrapper shell script that virtually is ssh -F /path/to/your/config $*
    3. Prepend GIT_SSH=/path/to/your/wrapper in front of your normal Git command. Here the git remote (origin) must use the git@github.com:user/project.git format.
    0 讨论(0)
  • 2020-11-21 05:04

    This works for me I'm using Windows 10

    git config --global credential.helper wincred
    
    0 讨论(0)
  • 2020-11-21 05:05

    Simply include the login credentials as part of the URL:

    git remote rm origin
    git remote add origin https://username:mypassword@github.com/path/to/repo.git
    

    Note: I do not recommend this method, but if you are in rush and nothing else works, you can use this method.

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