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

后端 未结 24 3262
囚心锁ツ
囚心锁ツ 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条回答
  • 2020-11-21 05:24

    If you don't want to store your password in plaintext like Mark said, you can use a different GitHub URL for fetching than you do for pushing. In your configuration file, under [remote "origin"]:

    url = git://github.com/you/projectName.git
    pushurl = git@github.com:you/projectName.git
    

    It will still ask for a password when you push, but not when you fetch, at least for open source projects.

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

    Things are a little different if you're using two-factor authentication as I am. Since I didn't find a good answer elsewhere, I'll stick one here so that maybe I can find it later.

    If you're using two-factor authentication, then specifying username/password won't even work - you get access denied. But you can use an application access token and use Git's credential helper to cache that for you. Here are the pertinent links:

    • Setting up the command-line to work with 2-factor auth (search for section titled "How does it work for command-line Git?")
    • Credential caching

    And I don't remember where I saw this, but when you're asked for your username - that's where you stick the application access token. Then leave the password blank. It worked on my Mac.

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

    There's an easy, old-fashioned way to store user credentials in an HTTPS URL:

    https://user:password@github.com/...
    

    You can change the URL with git remote set-url <remote-repo> <URL>

    The obvious downside to that approach is that you have to store the password in plain text. You can still just enter the user name (https://user@github.com/...) which will at least save you half the hassle.

    You might prefer to switch to SSH or to use the GitHub client software.

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

    Usually you have a remote URL, something like this,

    git remote -v
    
    origin    https://gitlab.com/username/Repo.git (fetch)
    origin    https://gitlab.com/username/Repo.git (push)
    

    If you want to skip username and password while using git push, try this:

     git remote set-url origin https://username:password@gitlab.com/username/Repo.git
    

    I've just added the same URL (with user details including password) to origin.

    NOTE: It doesn't work if username is an email Id.

    git remote -v
    
    origin    https://username:password@gitlab.com/username/Repo.git (fetch)
    origin    https://username:password@gitlab.com/username/Repo.git (push)
    
    0 讨论(0)
  • 2020-11-21 05:27

    You also edit the bashrc file and add a script in it.

    This would ask for your password once when you start Git and then remembers it until you log off.

    SSH_ENV=$HOME/.ssh/environment
      
    # Start the ssh-agent
    function start_agent {
        echo "Initializing new SSH agent..."
    
        # Spawn ssh-agent
        /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
        echo succeeded
        chmod 600 "${SSH_ENV}"
        . "${SSH_ENV}" > /dev/null
        /usr/bin/ssh-add
    }
      
    if [ -f "${SSH_ENV}" ]; then
         . "${SSH_ENV}" > /dev/null
       ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
          start_agent;
      }
    else
        start_agent;
    fi
    
    0 讨论(0)
  • 2020-11-21 05:28

    On a GNU/Linux setup, a ~/.netrc works quite well too:

    $ cat ~/.netrc
    machine github.com login lot105 password howsyafather
    

    It might depend on which network libraries Git is using for HTTPS transport.

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