Git push requires username and password

前端 未结 24 2034
灰色年华
灰色年华 2020-11-22 04:14

I cloned a Git repository from my GitHub account to my PC.

I want to work with both my PC and laptop, but with one GitHub account.

When I try to push to or p

相关标签:
24条回答
  • 2020-11-22 04:38

    Permanently authenticating with Git repositories

    Run the following command to enable credential caching:

    $ git config credential.helper store
    $ git push https://github.com/owner/repo.git
    
    Username for 'https://github.com': <USERNAME>
    Password for 'https://USERNAME@github.com': <PASSWORD>
    

    You should also specify caching expire,

    git config --global credential.helper 'cache --timeout 7200'
    

    After enabling credential caching, it will be cached for 7200 seconds (2 hour).

    0 讨论(0)
  • 2020-11-22 04:40

    Source: Set Up Git

    The following command will save your password in memory for some time (for Git 1.7.10 or newer).

    $ git config --global credential.helper cache
    # Set git to use the credential memory cache
    
    $ git config --global credential.helper 'cache --timeout=3600'
    # Set the cache to timeout after one hour (setting is in seconds)
    
    0 讨论(0)
  • 2020-11-22 04:44

    A common cause is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking "Clone or download", then clicking the "Use SSH" button above the URL field and updating the URL of your origin remote like this:

    git remote set-url origin git@github.com:username/repo.git
    

    This is documented at GitHub: Switching remote URLs from HTTPS to SSH.

    0 讨论(0)
  • 2020-11-22 04:44

    Updating your Git configuration file directly (if you do not want to memorize fancy commands):

    Open your .git/config file in your favorite text editor. It will be in the folder that you cloned or in the repository that you performed git init in. Go into that repository. .git is a hidden folder, and pressing Ctrl + H should show the hidden folder, (ls -a in terminal).

    Below is a sample of the .git/config file. Copy and paste these lines and be sure to update those lines with your Git information.

    [user]
            name = Tux
            email = tux@gmail.com
            username = happy_feet
    
    [remote "origin"]
            url = https://github.com/happy_feet/my_code.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    

    Change the URL part with the following format for SSH:

    url = git@github.com:happy_feet/my_code.git
    

    (The above formats do not change with various Git remote servers like GitHub or Bitbucket. It's the same if you are using Git for version control):

    Note: The SSH way of connecting to a remote Git repository will require you to add your public SSH key to your Git remote server (like GitHub or Bitbucket. Search the settings page for SSH keys).

    To know how to generate your SSH keys, refer to: Creating SSH keys

    0 讨论(0)
  • 2020-11-22 04:45

    If the SSH key or .netrc file did not work for you, then another simple, but less secure solution, that could work for you is git-credential-store - Helper to store credentials on disk:

    git config --global credential.helper store
    

    By default, credentials will be saved in file ~/.git-credentials. It will be created and written to.

    Please note using this helper will store your passwords unencrypted on disk, protected only by filesystem permissions. If this may not be an acceptable security tradeoff.

    0 讨论(0)
  • 2020-11-22 04:46

    If you are using Git (for example, Git Bash) under Windows (and if you don't want to switch from HTTPS to SSH), you could also use Git Credential Manager for Windows

    This application will keep the username and password for you...

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