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

For the uninitiated who are confused by the previous answers, you can do:

git remote -v

which will respond something like

origin  https://yourname@github.com/yourname/yourrepo.git (fetch)
origin  https://yourname@github.com/yourname/yourrepo.git (push)

then you can run the command many other have suggested, but now you know yourname and yourrepo from above, so you can just cut and paste yourname/yourrepo.git from above into

git remote set-url origin git@github.com:yourname/yourrepo.git
查看更多
忆尘夕之涩
3楼-- · 2018-12-31 09:15

Here's another option:

Instead of writing

git push origin HEAD

You could write:

git push https://user:pass@yourrepo.com/path HEAD

Obviously with most shells this will result in password getting cached in history, so keep that in mind.

查看更多
低头抚发
4楼-- · 2018-12-31 09:16

Source: Set Up Git

The following command will save your password in memory for sometime.
(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 1 hour (setting is in seconds)
查看更多
怪性笑人.
5楼-- · 2018-12-31 09:16

What worked for me was to edit .git/config and use

[remote "origin"]
        url = https://<login>:<password>@gitlab.com(...).git

It goes without saying that this is an insecure way of storing your password but there are environments/cases where this may not be a problem.

查看更多
其实,你不懂
6楼-- · 2018-12-31 09:17

I just came across the same problem, and the simplest solution I found was to use SSH URL instead of HTTPS one:

ssh://git@github.com/username/repo.git

And not this:

https://github.com/username/repo.git

You can now validate with just the SSH Key instead of the username and password.

查看更多
查无此人
7楼-- · 2018-12-31 09:17

This is what worked for me:

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

Example:

git remote set-url origin https://jsmith@github.com/jsmith/master.git
查看更多
登录 后发表回答