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:03

You need to perform 2 steps -

1) git remote remove origin
2) git remote add origin git@github.com:NuggetAI/nugget.git

Notice the git url is a ssh url and not an https url .. Which u can select from here.

enter image description here

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

If you are using git (ex. git bash) under Windows (and if you don't want to switch from https to ssh)

you could also use https://github.com/Microsoft/Git-Credential-Manager-for-Windows

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

查看更多
查无此人
4楼-- · 2018-12-31 09:04

If you're using ssh and your private key is encrypted with a passphrase, then you'll still be prompted to enter the passphrase/password for the private key when you do network operations with Git like push, pull, and fetch.

Use ssh-agent to save private key passphrase/password credentials

If you want to avoid having to enter your passphrase every time, you can use ssh-agent to store your private key passphrase credentials once per terminal session, as I explain in my answer to Could not open a connection to your authentication agent:

$ eval `ssh-agent -s`
$ ssh-add

In a Windows msysgit Bash, you need to evaluate the output of ssh-agent, but I'm not sure if you need to do the same in other development environments and operating systems.

ssh-add looks for a private key in your home .ssh folder called id_rsa, which is the default name, but you can pass a filepath to a key with a different name.

Killing the agent

When you're done with your terminal session, you can shutdown ssh-agent with the kill flag -k:

$ ssh-agent -k

As explained in the ssh-agent manual:

-k

Kill the current agent (given by the SSH_AGENT_PID environment variable).

Optional timeout

Also, it can take an optional timeout parameter like so:

$ ssh-add -t <timeout>

where <timeout> is of the format <n>h for <n> hours, <n>m for <n> minutes, and so on.

According to the ssh-agent manual:

-t life

Set a default value for the maximum lifetime of identities added to the agent. The lifetime may be specified in seconds or in a time format specified in sshd_config(5). A lifetime specified for an identity with ssh-add(1) overrides this value. Without this option the default maximum lifetime is forever.

See this page for more time formats.

Security warning for Cygwin users

Cygwin users should be aware of a potential security risk with using ssh-agent in Cygwin:

people should be cognizant of the potential dangers of ssh-agent under cygwin [1], though under a local netstat and remote portscan it does not appear that the port specified in /tmp/ssh-foo is accessible to anyone ...?

[1]: http://www.cygwin.com/ml/cygwin/2001-01/msg00063.html

And at the cited link:

however, note that cygwin's unix domain sockets are FUNDAMENTALLY INSECURE and so i strongly DISCOURAGE usage of ssh-agent under cygwin.

when you run ssh-agent under cygwin it creates AF_UNIX socket in /tmp/ssh-$USERNAME/ directory. under cygwin AF_UNIX sockets are emulated via AF_INET sockets. you can easily see that if you'll look into /tmp/ssh-$USERNAME/agent-socket-* file via notepad. you'll see the something like

!<socket >2080

then run netstat -a and surprise! you have some program listening to port 2080. it's ssh-agent. when ssh receives RSA challenge from server, it refers to corresponding /tmp/ssh-$USERNAME/agent-socket-* (under cygwin, in our case, that means it'll open connection to localhost:2080) and asks ssh-agent to process RSA challenge with private key it has, and then it simply passes response received from ssh-agent to server.

under unix, such scenario works without problems, because unix kernel checks permissions when program tries to access AF_UNIX socket. For AF_INET sockets, however, connections are anonymous (read "insecure"). Imagine, that you have cygwin ssh-agent running. malicious hacker may portscan your box, locate open port used by ssh-agent, open connection to your ssh server, receive RSA challenge from it, send it to your ssh-agent via open port he found, receive RSA response, send it to ssh server and voila, he successfully logged in to your server as you.

查看更多
与君花间醉酒
5楼-- · 2018-12-31 09:04

You basically have two options.

If you use the same user on both machines you need to copy the .pub key to your PC, so github knows that you are the same user.

If you have created a new .pub file for your PC and want to treat the machines as different users, you need to register the new .pub file on the github website.

If this still doesn't work it might be because ssh is not configured correctly and that ssh fail to find the location of your keys. Try

ssh -vv username@github.com

To get more information why SSH fails.

查看更多
牵手、夕阳
6楼-- · 2018-12-31 09:04

For windows git users, After running git config --global credential.helper store, if it still prompts for password, you'd better check where the config is written to. using this command

git config --list --show-origin

In my case, manually edit config file 'C:\Program Files\Git\mingw64\etc\gitconfig', adding the following text , it works.

[credential] helper = store

查看更多
残风、尘缘若梦
7楼-- · 2018-12-31 09:05

Permanently authenticating with Git repositories,

Run following command to enable credential caching:

$ git config credential.helper store
$ git push https://github.com/repo.git

Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>

Use 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).

查看更多
登录 后发表回答