Git push requires username and password

2018-12-31 08:31发布

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 pull from Github using my PC, it requires username and password, but not when using the laptop!

I don't want to type my username and password every time I interact with origin. What I am missing here?

20条回答
时光乱了年华
2楼-- · 2018-12-31 09:06

As many users has said, you just have to change your git repositorie URL from https to SSH.

If you haven't generated a SSH key in your machine, then your are going to have to do it.

Just as an additional information, after doing this change I still was getting the same error: Permission Denied.

In my case, the problem was that I was using the Windows Shell to execute the ngh command; since this command should open a prompt to request the SSH phrase and the Windows Shell doesn't open this kinds of prompts, the authentication just failed.

So, I just had to open the git shell and execute the ngh command there, put the SSH phrase in the prompt every time it asked for it and "voilà"... It just worked fine!

查看更多
时光乱了年华
3楼-- · 2018-12-31 09:09

You can cache your GitHub password in Git:

Just follow the instructions from the github's official documentation.

After following the instructions from the above link, you should be able to push/pull to/from your repo without typing your username/password every time.

查看更多
临风纵饮
4楼-- · 2018-12-31 09:09

If the ssh key or .netrc 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 ~/.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.

查看更多
若你有天会懂
5楼-- · 2018-12-31 09:11

Update for HTTPS:

Github has launched a new program for Windows that stores your credentials when you're using HTTPS:

To use:

Download the program from here

Once you run the program it will edit your .gitconfig file. Recheck if it edited the correct .gitconfig in case you have several of them. If it didn't edit the correct one, add the following to your .gitconfig

[credential]
    helper = !'C:\\Path\\To\\Your\\Downloaded\\File\\git-credential-winstore.exe'

NOTE the line break after [credential]. It is required.

Open up your command line client and try git push origin master once. If it asks you for a password, enter it and you're through. Password saved!

查看更多
忆尘夕之涩
6楼-- · 2018-12-31 09:13

Apart from changing to SSH you can also keep using HTTPS, if you don't mind to put your password in clear text. Put this in your ~/.netrc and it won't ask for your username/password (at least on Linux and Mac):

machine github.com
       login <user>
       password <password>

Addition (see VonC's 2nd comment): on Windows the file name is %HOME%\_netrc.

Also read VonC's first comment in case you want to encrypt.

Another addition (see user137717's comment) which you can use if you have git 1.7.10 or newer.

Cache your github password in git using a credential helper :

If you're cloning GitHub repositories using HTTPS, you can use a credential helper to tell Git to remember your GitHub username and password every time it talks to GitHub.

This also works on Linux, Mac and Windows.

查看更多
还给你的自由
7楼-- · 2018-12-31 09:13

I had the same same issue.

so change the .git/config file from my project

url = https://github.com/<your-user-here>/<your-repo-here>

to

url = git@github.com:<your-user-here>/<your-repo-here>

and add the ssh public key to the git profile which is in setting.

for ssh public key

cat ~/.ssh/id_rsa.pub
查看更多
登录 后发表回答