git 为什么 Github 在按照屏幕上的说明操作并推送新的 repo 时会要求输入用户名/密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10909221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:51:42  来源:igfitidea点击:

Why is Github asking for username/password when following the instructions on screen and pushing a new repo?

gitgithub

提问by Kit Sunde

I'm the owner of an organization on github and just created a repo and tried pushing but I'm running into an issue where it's asking me for my username even though I can SSH just fine:

我是 github 上一个组织的所有者,刚刚创建了一个 repo 并尝试推送,但我遇到了一个问题,它要求我提供我的用户名,即使我可以 SSH 就好了:

$ ssh -T [email protected]
Hi Celc! You've successfully authenticated, but GitHub does not provide shell access.
$ git add .
$ git commit -m 'first commit'
[master (root-commit) 3f1b963] first commit
 6 files changed, 59 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 main.js
 create mode 100644 package.json
 create mode 100644 readme.markdown
 create mode 100644 views/index.ejs
 create mode 100644 views/layout.ejs
$ git remote add origin https://github.com/WEMP/project-slideshow.git
$ git push -u origin master
Username for 'https://github.com':

What am I doing wrong? This has never happened to me before but I recently also upgraded to git 1.7.10.3.

我究竟做错了什么?我以前从未发生过这种情况,但我最近也升级到了 git 1.7.10.3。

回答by Hugo

Don't use HTTP use SSH instead

不要使用 HTTP,而是使用 SSH

change

改变

https://github.com/WEMP/project-slideshow.git 

to

[email protected]:WEMP/project-slideshow.git

you can do it in .git/configfile

你可以在.git/config文件中做到

回答by Sean

I had this same issue and wondered why it didn't happen with a bitbucket repo that was cloned with https. Looking into it a bit I found that the config for the BB repo had a URL that included my username. So I manually edited the config for my GH repo like so and voila, no more username prompt. I'm on Windows.

我遇到了同样的问题,想知道为什么用 https 克隆的 bitbucket 存储库没有发生这种情况。仔细研究了一下,我发现 BB 存储库的配置有一个包含我的用户名的 URL。所以我像这样手动编辑了我的 GH 存储库的配置,瞧,没有更多的用户名提示。我在 Windows 上。

Edit your_repo_dir/.git/config(remember: .gitfolder is hidden)

编辑your_repo_dir/.git/config(记住:.git文件夹是隐藏的)

Change:

改变:

https://github.com/WEMP/project-slideshow.git

to:

到:

https://*username*@github.com/WEMP/project-slideshow.git

Save the file. Do a git pullto test it.

保存文件。做一个git pull来测试它。

The proper way to do this is probably by using git bash commands to edit the setting, but editing the file directly didn't seem to be a problem.

正确的方法可能是使用 git bash 命令来编辑设置,但直接编辑文件似乎没有问题。

回答by Catalin Hritcu

Here is an official answerto this:

以下是对此的官方回答

If Git prompts you for a username and password every time you try to interact with GitHub, you're probably using the HTTPS clone URL for your repository.

Using an HTTPS remote URLhas some advantages: it's easier to set up than SSH, and usually works through strict firewalls and proxies. However, it also prompts you to enter your GitHub credentials every time you pull or push a repository.

You can configure Git to store your password for you. If you'd like to set that up, read all about setting up password caching.

如果每次尝试与 GitHub 交互时 Git 都提示您输入用户名和密码,则您可能正在为您的存储库使用 HTTPS 克隆 URL。

使用HTTPS 远程 URL有一些优点:它比 SSH 更容易设置,并且通常可以通过严格的防火墙和代理。但是,它还会在您每次拉取或推送存储库时提示您输入 GitHub 凭据。

您可以配置 Git 来为您存储密码。如果您想进行设置,请阅读有关设置密码缓存的所有信息。

回答by kaushal

Improving upon @Ianl's answer,

改进@Ianl 的回答

It seems that if 2-step authentication is enabled, you have to use token instead of password. You could generate a token here.

似乎如果启用了两步验证,您必须使用令牌而不是密码。您可以在此处生成令牌。

If you want to disable the prompts for both the username and password then you can set the URL as follows -

如果要禁用用户名和密码的提示,则可以按如下方式设置 URL -

git remote set-url origin https://username:[email protected]/WEMP/project-slideshow.git

Note that the URL has both the username and password. Also the .git/configfile should show your current settings.

请注意,该 URL 包含用户名和密码。该.git/config文件还应显示您当前的设置。



Update 20200128:

更新 20200128:

If you don't want to store the password in the config file, then you can generate your personal token and replace the password with the token. Here are some details.

如果您不想将密码存储在配置文件中,那么您可以生成您的个人令牌并将密码替换为该令牌。这里有一些细节

It would look like this -

它看起来像这样 -

git remote set-url origin https://username:[email protected]/WEMP/project-slideshow.git

回答by IanI

an additional note:

补充说明:

if you have already added a remote ($git remote add origin ... ) and need to change that particular remote then do a remote remove first ($ git remote rm origin), before re-adding the new and improved repo URL (where "origin" was the name for the remote repo).

如果您已经添加了一个遥控器($git remote add origin ...)并且需要更改该特定遥控器,那么在重新添加新的和改进的 repo URL(其中“origin”是远程仓库的名称)。

so to use the original example :

所以使用原始示例:

$ git remote add origin https://github.com/WEMP/project-slideshow.git
$ git remote rm origin
$ git remote add origin https://[email protected]/WEMP/project-slideshow.git

回答by a paid nerd

If you're using HTTPS, check to make sure that your URL is correct. For example:

如果您使用 HTTPS,请检查以确保您的 URL 正确。例如:

$ git clone https://github.com/wellle/targets.git
Cloning into 'targets'...
Username for 'https://github.com': ^C

$ git clone https://github.com/wellle/targets.vim.git
Cloning into 'targets.vim'...
remote: Counting objects: 2182, done.
remote: Total 2182 (delta 0), reused 0 (delta 0), pack-reused 2182
Receiving objects: 100% (2182/2182), 595.77 KiB | 0 bytes/s, done.
Resolving deltas: 100% (1044/1044), done.

回答by jochen

I've just had an email from a github.com admin stating the following: "We normally advise people to use the HTTPS URL unless they have a specific reason to be using the SSH protocol. HTTPS is secure and easier to set up, so we default to that when a new repository is created."

我刚刚收到一封来自 github.com 管理员的电子邮件,内容如下:“我们通常建议人们使用 HTTPS URL,除非他们有使用 SSH 协议的特定原因。HTTPS 安全且易于设置,因此我们在创建新存储库时默认使用它。”

The password prompt does indeed accept the normal github.com login details. A tutorial on how to set up password caching can be found at https://help.github.com/articles/set-up-git#password-caching. I followed the steps in the tutorial, and it worked for me.

密码提示确实接受正常的 github.com 登录详细信息。关于如何设置密码缓存的教程可以在https://help.github.com/articles/set-up-git#password-caching找到。我按照教程中的步骤操作,它对我有用。

回答by Hazarapet Tunanyan

Because you are using HTTPSway.HTTPSrequires that you type your account access every time you try to push or pull,but there is one way too, called SSH, and it lets you to tell git, that I give you permission with my account for this pc, and never ask me again about any user access. To use it, you have to generate SSHkey and add it into your Github's account just one time.To do that, you can follow these steps

因为您使用的是HTTPS方式。HTTPS要求您每次尝试推送或拉取时都输入您的帐户访问权限,但也有一种方法,称为SSH,它可以让您告诉 git,我已授予您使用我的帐户使用这台电脑的权限,并且从不询问我再次关于任何用户访问。要使用它,您必须生成 SSH密钥并将其添加到您的 Github 帐户中一次。为此,您可以按照以下步骤操作

How To Generate SSH key for Github

如何为 Github 生成 SSH 密钥

回答by Neil

If you've enabled two factor authentication, then you'll need to generate a personal access token and use that instead of your regular password. More info here: https://help.github.com/articles/creating-an-access-token-for-command-line-use/

如果您启用了双因素身份验证,则需要生成个人访问令牌并使用它而不是常规密码。更多信息在这里:https: //help.github.com/articles/creating-an-access-token-for-command-line-use/