Git推送需要用户名和密码

发布于 2024-11-18 01:42:35 字数 201 浏览 10 评论 0 原文

我将 Git 存储库从我的 GitHub 帐户克隆到我的 PC。

我想同时使用我的 PC 和笔记本电脑,但使用一个 GitHub 帐户。

当我尝试使用 PC 向 GitHub 推送或拉取时,它需要用户名和密码,但当我使用笔记本电脑时则不需要!

我不想每次与origin 交互时都输入用户名和密码。我在这里缺少什么?

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 a username and password, but not when I'm using the laptop!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

绅刃 2024-11-25 01:42:35

一个常见原因是使用默认 (HTTPS) 而不是 SSH 进行克隆。您可以通过以下方式纠正此问题:转到您的存储库,单击“克隆或下载”,然后单击 URL 字段上方的“使用 SSH”按钮并更新原始远程的 URL,如下所示:

git remote set-url origin [email protected]:username/repo.git

您可以检查是否已将远程添加为使用 HTTPS 或 SSH:

git remote -v

这在 GitHub 上有记录:将远程 URL 从 HTTPS 切换到 SSH

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 [email protected]:username/repo.git

You can check if you have added the remote as HTTPS or SSH using:

git remote -v

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

一个人练习一个人 2024-11-25 01:42:35

使用 Git 存储库永久进行身份验证

运行以下命令以启用 凭证缓存

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

Username for 'https://github.com': <USERNAME>
Password for 'https://[email protected]': <PASSWORD>

您还应该指定缓存过期

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

启用凭证缓存后,它将被缓存7200秒(2小时)。 43,200 秒 = 12 小时(每天登录一次)对于某些人来说也可能是一个合理的选择。


正如 SebastianH 评论的那样,也可以使用类似的命令来配置它,而不是针对每个存储库,而是全局配置:

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

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://[email protected]': <PASSWORD>

You should also specify caching expire,

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

After enabling credential caching, it will be cached for 7200 seconds (2 hour). 43,200 s = 12 h (login once per day) might also be a reasonable choice for some.


As SebastianH commented, can also use similar commands to configure this not per repository but globally:

git config --global credential.helper store
git config --global credential.helper 'cache --timeout 7200'
水波映月 2024-11-25 01:42:35

我刚刚遇到了同样的问题,我发现的最简单的解决方案是使用 SSH URL 而不是 HTTPS:

ssh://[email protected]/username/repo.git

而不是这样:

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

您现在可以使用 SSH 密钥而不是 进行验证用户名密码

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

ssh://[email protected]/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.

岁月如刀 2024-11-25 01:42:35

除了更改为 SSH 之外,如果您不介意以明文形式输入密码,还可以继续使用 HTTPS。将其放入您的 ~/.netrc 中,它不会询问您的用户名/密码(至少在 Linux 和 Mac 上):

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

添加(请参阅 VonC 的第二条评论):在 Windows 上,文件名为 %HOME%\_netrc

如果您想加密,另请阅读 VonC 的第一条评论。

另一个补充(请参阅 user137717 的评论),如果您有 Git 1.7.10 或更高版本,则可以使用它。

使用凭证助手在 Git 中缓存 GitHub 密码 :

如果您使用 HTTPS 克隆 GitHub 存储库,则可以使用
凭据助手告诉 Git 记住您的 GitHub 用户名和
每次与 GitHub 对话时都要输入密码。

这也适用于 Linux、Mac 和 Windows。

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 second 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.

向地狱狂奔 2024-11-25 01:42:35

对于那些对前面的答案感到困惑的新手,您可以这样做:

git remote -v

它将响应类似的内容

origin    https://[email protected]/yourname/yourrepo.git (fetch)
origin    https://[email protected]/yourname/yourrepo.git (push)

然后您可以运行许多其他人建议的命令,但现在您从上面知道了您的名字和您的存储库,因此您可以剪切并粘贴< code>yourname/yourrepo.git 从上面进入:

git remote set-url origin [email protected]:yourname/yourrepo.git

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

git remote -v

Which will respond with something like

origin    https://[email protected]/yourname/yourrepo.git (fetch)
origin    https://[email protected]/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 the above into:

git remote set-url origin [email protected]:yourname/yourrepo.git
乖乖兔^ω^ 2024-11-25 01:42:35

如果您使用 SSH 并且您的私钥是使用密码加密的,那么当您使用 Git 进行网络操作时,系统仍会提示您输入私钥的密码/密码 例如获取

使用 ssh-agent 保存私钥密码/密码凭据

如果您想避免每次都输入密码,可以使用 ssh-agent 在每个终端会话中存储一次私钥密码凭据,正如我在我对无法打开到的连接的回答你的身份验证代理

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

在Windows msysgit Bash中,你需要评估ssh-agent的输出,但我不是确定您是否需要在其他开发环境和操作系统中执行相同的操作。

ssh-add 在您的主 .ssh 文件夹中查找名为 id_rsa 的私钥,这是默认名称,但您可以传递文件路径到具有不同名称的密钥。

终止代理

当您完成终端会话后,您可以使用终止标志 -k 关闭 ssh-agent

$ ssh-agent -k

ssh-agent 手册

<前><代码>-k

终止当前代理(由 SSH_AGENT_PID 环境变量指定)。

可选超时

此外,它还可以采用可选超时参数,如下所示:

$ ssh-add -t <timeout>

其中 的格式为 h(对于 小时,m 表示 分钟,依此类推。

根据ssh-agent手册< /a>:

<前><代码>-t 生活

为添加到代理的身份的最大生命周期设置默认值。生命周期可以以秒或以秒为单位指定
sshd_config(5)。使用 ssh-add(1) 覆盖此值。如果没有此选项,默认的最大生命周期是永远的。

请参阅此页面了解更多时间格式

Cygwin 用户的安全警告

Cygwin 用户应意识到使用时存在的潜在安全风险Cygwin 中的 ssh 代理

人们应该认识到
Cygwin 下 ssh-agent 的潜在危险 1< /a>,尽管在​​本地 netstat 和远程 portscan 下,似乎任何人都无法访问 /tmp/ssh-foo 中指定的端口...?

<前><代码>[1]:http://www.cygwin.com/ml/cygwin/2001-01/msg00063.html

引用的链接中:

但是,请注意,Cygwin 的 Unix 域套接字根本上不安全,因此我强烈禁止在 Cygwin 下使用 ssh-agent。

当您在 Cygwin 下运行 ssh-agent 时,它会在 /tmp/ssh-$USERNAME/ 目录中创建 AF_UNIX 套接字。在 Cygwin 下,AF_UNIX 套接字通过 AF_INET 套接字进行模拟。如果您通过记事本查看 /tmp/ssh-$USERNAME/agent-socket-* 文件,您可以轻松看到这一点。你会看到类似的东西

!<套接字>2080

然后运行netstat -a,结果令人惊讶!你有一些程序正在监听 2080 端口。它就是 ssh-agent。当 ssh 收到来自服务器的 RSA 质询时,它会引用相应的 /tmp/ssh-$USERNAME/agent-socket-* (在 Cygwin 下,在我们的例子中,这意味着它将打开到localhost:2080) 并要求 ssh-agent 使用其拥有的私钥处理 RSA 质询,然后简单地将从 ssh-agent 收到的响应传递到服务器。

在 Unix 下,这种情况不会出现任何问题,因为当程序尝试访问 AF_UNIX 套接字时,Unix 内核会检查权限。然而,对于 AF_INET 套接字,连接是匿名的(读
“不安全”)。想象一下,您正在运行 Cygwin ssh 代理。恶意黑客可能会对您的机器进行端口扫描,找到 ssh-agent 使用的开放端口,打开与您的 SSH 服务器的连接,从其接收 RSA 质询,通过他/她发现的开放端口将其发送到您的 ssh-agent,接收RSA 响应,将其发送到 SSH 服务器,瞧,他/她以您的身份成功登录到您的服务器。

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 the 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 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 an RSA challenge from the 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 the RSA challenge with the private key it has, and then it simply passes the response received from the ssh-agent to the server.

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

莫多说 2024-11-25 01:42:35

来源:设置 Git

以下命令将保存您的密码会在内存中保存一段时间(对于 Git 1.7.10 或更高版本)。

$ 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)

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)
疏忽 2024-11-25 01:42:35

当您使用 https 进行 Git pull 时推送时,只需为您的项目配置 remote.origin.url 即可,以避免每次推送时都输入用户名(或/和密码)。

如何配置remote.origin.url

URL format:
    https://{username:password@}github.com/{owner}/{repo}

Parameters in URL:

* username 
Optional, the username to use when needed.
authentication, if specified, no need to enter username again when need authentication. Don't use email; use your username that has no "@", otherwise the URL can't be parsed correctly, * password optional, the password to use when need authentication. If specified, there isn't any need to enter the password again when needing authentication. Tip: this value is stored as plain text, so for security concerns, don't specify this parameter, * e.g git config remote.origin.url https://[email protected]/eric/myproject

@Update - 使用ssh

我认为使用ssh协议更好比 https 更好的解决方案,尽管设置步骤稍微复杂一些。

大致步骤:

  • 使用命令创建ssh密钥,例如Linux上的ssh-keygen,Windows上的msysgit提供类似的命令。
  • 将私钥保存在本地计算机上的适当位置,例如~/.ssh。并通过 ssh-add 命令将其添加到 ssh 代理。
  • 将公钥上传到 Git 服务器。
  • 将 Git 存储库的 remote.origin.url 更改为 ssh 样式,例如 [email protected]:myaccount/myrepo.git
  • 然后,在拉取或推送时,无需输入用户名或密码。

提示

  • 如果您的 ssh 密钥有密码,则默认情况下,您在每次重新启动计算机后首次使用该密钥时都需要输入该密码。

@Update - 在 httpsssh 协议之间切换。

只需更改 remote.origin.url 就足够了,或者您可以直接编辑 repo_home/.git/config 来更改值(例如使用 vi 在 Linux 上)。

通常我为每个协议添加一行,并使用 # 注释掉其中一个协议。

例如

[remote "origin"]
        url = [email protected]:myaccount/myrepo.git
        # url = https://[email protected]/myaccount/myrepo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

When you use https for Git pull & push, just configure remote.origin.url for your project, to avoid input username (or/and password) every time you push.

How to configure remote.origin.url:

URL format:
    https://{username:password@}github.com/{owner}/{repo}

Parameters in URL:

* username 
Optional, the username to use when needed.
authentication, if specified, no need to enter username again when need authentication. Don't use email; use your username that has no "@", otherwise the URL can't be parsed correctly, * password optional, the password to use when need authentication. If specified, there isn't any need to enter the password again when needing authentication. Tip: this value is stored as plain text, so for security concerns, don't specify this parameter, * e.g git config remote.origin.url https://[email protected]/eric/myproject

@Update - using ssh

I think using ssh protocol is a better solution than https, even though the setup step is a little more complex.

Rough steps:

  • Create ssh keys using command, e.g ssh-keygen on Linux, on windows msysgit provide similar commands.
  • Keep the private key on the local machine at a proper location, e.g., ~/.ssh. And add it to the ssh agent via ssh-add command.
  • Upload the public key to the Git server.
  • Change remote.origin.url of the Git repository to ssh style, e.g., [email protected]:myaccount/myrepo.git
  • Then when pull or push, there isn't any need to enter the username or password ever.

Tips:

  • If your ssh key has a passphrase, then you need to input it on first use of the key after each restart of your machine, by default.

@Update - Switch between https and ssh protocol.

Simply changing remote.origin.url will be enough, or you can edit repo_home/.git/config directly to change the value (e.g using vi on Linux).

Usually I add a line for each protocol, and comment out one of them using #.

E.g.

[remote "origin"]
        url = [email protected]:myaccount/myrepo.git
        # url = https://[email protected]/myaccount/myrepo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
舂唻埖巳落 2024-11-25 01:42:35

如果您在 Github 帐户上启用了 2FA,则您的常规密码将无法用于此目的,但您可以生成个人访问令牌并在其位置上使用它。

访问设置 -> 开发者设置 -> GitHub 中的个人访问令牌 页面 (https://github.com/settings/tokens /new),并生成具有所有 Repo 权限的新令牌:

<图片src="https://i.sstatic.net/hEuYv.png" alt="生成 GitHub 个人访问令牌">

然后页面将显示新的令牌值。保存此值,并在推送到 GitHub 上的存储库时使用它代替密码:

> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>

If you've got 2FA enabled on your Github account, your regular password won't work for this purpose, but you can generate a Personal Access Token and use that in its place instead.

Visit the Settings -> Developer Settings -> Personal Access Tokens page in GitHub (https://github.com/settings/tokens/new), and generate a new Token with all Repo permissions:

generate GitHub personal access token

The page will then display the new token value. Save this value and use it in place of your password when pushing to your repository on GitHub:

> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>
好菇凉咱不稀罕他 2024-11-25 01:42:35

您可以在 Git 中缓存您的 GitHub 密码:

只需按照 GitHub 的 官方文档

按照上述链接中的说明进行操作后,您应该能够向存储库推送/拉取存储库,而无需每次都输入用户名/密码。

You can cache your GitHub password in Git:

Just follow the instructions from GitHub's official documentation.

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

无法言说的痛 2024-11-25 01:42:35

对我有用的是编辑 .git/config 并使用

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

不用说,这是一种不安全的存储密码的方式,但在某些环境/情况下这可能不是问题。

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.

伊面 2024-11-25 01:42:35

这是另一个选项:

显然,对于

git push origin HEAD

您可以写:

git push https://user:[email protected]/path HEAD

大多数 shell,这将导致密码被缓存在历史记录中,因此请记住这一点。

Here's another option:

Instead of writing

git push origin HEAD

You could write:

git push https://user:[email protected]/path HEAD

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

梓梦 2024-11-25 01:42:35

对于 Mac OS,

  1. 请转到您的 Github Settings -> 开发者设置 -> GitHub 中的个人访问令牌 页面 (https://github.com/settings/tokens /new),并生成一个具有所有 Repo 权限的新令牌
  2. 在您的 mac 中搜索钥匙串访问 ->搜索 github.com ->点击显示密码,然后粘贴您刚刚复制的令牌
  3. 转到 CLI,它会再次要求您输入用户名和密码,输入您的 Github 用户名并将令牌粘贴为密码,然后您就可以在其余使用 CLI 的时间中继续使用了。

For Mac OS

  1. Go to your Github Settings -> Developer Settings -> Personal Access Tokens page in GitHub (https://github.com/settings/tokens/new), and generate a new Token with all Repo permissions
  2. Search Keychain Access in your mac -> search for github.com -> click Show password then paste the token you just copied.
  3. Go to the CLI, it will again ask for your username and password, enter your Github username and paste the token as password, and you should be good to go for the rest of the times you are using the CLI.
骄傲 2024-11-25 01:42:35

这是我的工作解决方案,它也通过了 2FA。

  1. 在浏览器上登录 github。
  2. 转到此页面并创建令牌
    https://github.com/settings/tokens
  3. 复制生成的令牌字符串。
  4. 在终端中输入此命令并使用您自己的值

git remote set-url origin https://{yourAccountName}:{theCopiedToken}@github.com/{yourAccountName}/{repositoryName}.git

现在您已登录并可以推/拉等。

Here's my working solution which passes 2FA as well.

  1. Login to github on your browser.
  2. Go to this page and create a token
    https://github.com/settings/tokens
  3. Copy the generated token string.
  4. Enter this command in terminal with your own values

git remote set-url origin https://{yourAccountName}:{theCopiedToken}@github.com/{yourAccountName}/{repositoryName}.git

Now you are logged in and can push/pull, etc.

╭⌒浅淡时光〆 2024-11-25 01:42:35

如果 SSH 密钥或 .netrc 文件不适合您,那么另一个简单但不太安全的解决方案可能适合您:git-credential-store - 在磁盘上存储凭据的帮助程序:

git config --global credential.helper store

默认情况下,凭据将保存在文件 ~/.git-credentials 中。它将被创建并写入。

请注意,使用此帮助程序会将您的密码未加密地存储在磁盘上,仅受文件系统权限的保护。如果这可能不是可接受的安全权衡。

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.

爱你不解释 2024-11-25 01:42:35

我有同样的问题。

因此,我将项目中的 .git/config 文件更改

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

url = [email protected]:<your-user-here>/<your-repo-here>

SSH 公钥,并将其添加到设置中的 Git 配置文件中。

对于 SSH 公钥:

cat ~/.ssh/id_rsa.pub

I had the same issue.

So I changed the .git/config file from my project,

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

to

url = [email protected]:<your-user-here>/<your-repo-here>

and added the SSH public key to the Git profile which is in setting.

For the SSH public key:

cat ~/.ssh/id_rsa.pub
杀お生予夺 2024-11-25 01:42:35

如果您克隆了 HTTPS 而不是 SSH,并且在拉取、推送和获取时面临用户名和密码提示问题。您只需使用 UBUNTU

步骤 1 即可解决此问题:
移动到根目录

cd ~/

创建一个文件 .git-credentials

将此内容添加到该文件中 usename passwordgithosting URL

https://user:[email protected]

执行命令

git config --global credential.helper store

然后立即 您将能够毫无麻烦地从您的存储库中拉出推送并获取所有详细信息。

If you have cloned HTTPS instead of SSH and facing issue with username and password prompt on pull, push and fetch. You can solve this problem simply for UBUNTU

Step 1:
move to root directory

cd ~/

create a file .git-credentials

Add this content to that file with you usename password and githosting URL

https://user:[email protected]

Then execute the command

git config --global credential.helper store

Now you will be able to pull push and fetch all details from your repo without any hassle.

怪我入戏太深 2024-11-25 01:42:35

直接更新您的 Git 配置文件(如果您不想记住花哨的命令):

用您喜欢的文本打开 .git/config 文件编辑。它将位于您克隆的文件夹中或您在其中执行 git init 的存储库中。进入该存储库。 .git 是一个隐藏文件夹,按 Ctrl + H 应显示隐藏文件夹,(ls -a在终端)。

以下是 .git/config 文件的示例。复制并粘贴这些行,并确保使用您的 Git 信息更新这些行。

[user]
        name = Tux
        email = [email protected]
        username = happy_feet

[remote "origin"]
        url = https://github.com/happy_feet/my_code.git
        fetch = +refs/heads/*:refs/remotes/origin/*

将 URL 部分更改为 SSH 的以下格式:(

url = [email protected]:happy_feet/my_code.git

上述格式不会因各种 Git 远程服务器(如 GitHub 或 Bitbucket)而改变。如果您使用 Git 进行版本控制,情况是一样的):

注意:连接到远程 Git 存储库的 SSH 方式将要求您将公共 SSH 密钥添加到 Git 远程服务器(例如 GitHub 或 Bitbucket)。在设置页面中搜索 SSH 密钥)。

要了解如何生成 SSH 密钥,请参阅:
创建 SSH 密钥

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 = [email protected]
        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 = [email protected]: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

无言温柔 2024-11-25 01:42:35

这对我有用:

git remote set-url origin https://[email protected]/username/reponame.git

示例:

git remote set-url origin https://[email protected]/jsmith/master.git

This is what worked for me:

git remote set-url origin https://[email protected]/username/reponame.git

Example:

git remote set-url origin https://[email protected]/jsmith/master.git
挽手叙旧 2024-11-25 01:42:35

你基本上有两个选择。

如果您在两台计算机上使用相同的用户,则需要将 .pub 密钥复制到您的 PC,以便 GitHub 知道您是同一用户。

如果您已为 PC 创建了新的 .pub 文件并希望将计算机视为不同的用户,则需要在 GitHub 网站上注册新的 .pub 文件。

如果这仍然不起作用,可能是因为 ssh 配置不正确,并且 ssh 无法找到密钥的位置。尝试

ssh -vv [email protected]

获取 SSH 失败原因的更多信息。

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 [email protected]

To get more information why SSH fails.

万人眼中万个我 2024-11-25 01:42:35

对于 Windows Git 用户,运行 git config --global credential.helper store 后,如果仍然提示输入密码,最好检查配置文件写入的位置,使用此命令

git config --list --show-origin

在我的在这种情况下,手动编辑配置文件“C:\Program Files\Git\mingw64\etc\gitconfig”并添加以下文本后,它起作用了。

[credential]
    helper = store

For Windows Git users, after running git config --global credential.helper store, if it still prompts for a password, you'd better check where the configuration file is written to, using this command

git config --list --show-origin

In my case, after manually editing configuration file 'C:\Program Files\Git\mingw64\etc\gitconfig', and adding the following text, it worked.

[credential]
    helper = store
朕就是辣么酷 2024-11-25 01:42:35

列出您当前的 SSH 密钥:

ls -l ~/.ssh

生成新的 SSH 密钥:

ssh-keygen -t ed25519 -C "[email protected]"

您应该在其中替换 [email  ;protected] 使用您的 GitHub 电子邮件
地址。
当提示输入要保存密钥的文件时,按
输入
输入密码后(无密码则为空) - 只需按
输入(输入空密码)。
再次列出您的 SSH 密钥:

ls -l ~/.ssh

文件 id_ed25519id_ed25519.pub 现在应该已添加。
在后台启动 ssh-agent:

eval $(ssh-agent -s)

将您的 SSH 私钥添加到 ssh-agent:

ssh-add ~/.ssh/id_ed25519

接下来将公钥输出到终端屏幕:

cat ~/.ssh/id_ed25519.pub

将输出复制到剪贴板
Ctrl + 插入)。
转到 https://github.com/ 并使用您的帐户登录
用户名和密码。
单击右上角的 GitHub 头像,然后单击设置
在左侧窗格中单击SSH 和 GPG 密钥
单击绿色按钮新建 SSH 密钥
并将公共 SSH 密钥粘贴到标记为 Key 的文本区域中。
使用描述性标题来说明您将在哪台计算机上使用
使用此 SSH 密钥。单击添加 SSH 密钥


如果您当前的本地存储库是使用 http用户名 创建的,
需要重新创建它才能兼容 SSH。
首先检查以确保您有一个干净的工作树
这样您就不会丢失任何工作:

git status

然后 cd .. 到父目录并 rm -fr
最后克隆一个使用 SSH 而不是用户名/密码的新副本:

git clone [email protected]:[your-github-username]/[repository-name].git

参考:
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/generate-a-new-ssh-key-and-adding-it-到 ssh 代理
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-帐户

List your current SSH keys:

ls -l ~/.ssh

Generate a new SSH key:

ssh-keygen -t ed25519 -C "[email protected]"

where you should replace [email protected] with your GitHub email
address.
When prompted to Enter a file in which to save the key, press
Enter.
Upon Enter passphrase (empty for no passphrase) - just press
Enter (for an empty passphrase).
List the your SSH keys again:

ls -l ~/.ssh

The files id_ed25519 and id_ed25519.pub should now have been added.
Start the ssh-agent in the background:

eval $(ssh-agent -s)

Add your SSH private key to the ssh-agent:

ssh-add ~/.ssh/id_ed25519

Next output the public key to the terminal screen:

cat ~/.ssh/id_ed25519.pub

Copy the output to the clipboard
(Ctrl + Insert).
Go to https://github.com/<your-github-username> and sign in with your
username and password.
Click your GitHub avatar in the upper-right corner, and then Settings.
In the left pane click SSH and GPG keys.
Click the green-colored button New SSH key
and paste the public SSH key into the textarea labeled Key.
Use a descriptive Title that tells from what computer you will
use this SSH key. Click Add SSH key.


If your current local repository was created with http and username,
it needs to be recreated it so as to become SSH compatible.
First check to make sure that you have a clean working tree
so that you don't lose any work:

git status

Then cd .. to the parent directory and rm -fr <name-of-your-repo>.
Finally clone a fresh copy that uses SSH instead of username/password:

git clone [email protected]:[your-github-username]/[repository-name].git

References:
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

萌无敌 2024-11-25 01:42:35

HTTPS 更新

GitHub 推出了一个适用于 Windows 的新程序,可在您使用 HTTPS 时存储您的凭据:

使用方法:

  • 此处

  • 运行该程序后,它将编辑您的 .gitconfig 文件。如果您有多个,请重新检查它是否编辑了正确的 .gitconfig。如果没有编辑正确的内容,请将以下内容添加到您的 .gitconfig

    <前><代码>[凭据]
    helper = !'C:\\Path\\To\\Your\\Downloaded\\File\\git-credential-winstore.exe'

    注意[credential]后面的换行符。这是必需的。

  • 打开命令行客户端并尝试一次git push origin master。如果它要求您输入密码,请输入密码即可完成。密码已保存!

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!

夜吻♂芭芘 2024-11-25 01:42:35

您需要执行两个步骤 -

  1. gitmoteremoveorigin
  2. gitremoteaddorigin[电子邮件受保护]:NuggetAI/nugget.git

请注意,Git URL 是 SSH URL,而不是 HTTPS URL...您可以从此处选择:

< img src="https://i.sstatic.net/kmjwG.png" alt="在此处输入图像描述">

You need to perform two steps -

  1. git remote remove origin
  2. git remote add origin [email protected]:NuggetAI/nugget.git

Notice the Git URL is a SSH URL and not an HTTPS URL... Which you can select from here:

Enter image description here

千里故人稀 2024-11-25 01:42:35

截至 2021 年,HTTPS 遥控器将有一个安全、用户友好的跨平台解决方案。不再需要输入密码!不再需要 SSH 密钥!不再需要个人访问令牌!

安装 GitHub 开发的 Git Credential Manager (下载)。它支持对 GitHub、BitBucket、Azure 和 GitLab 进行无密码浏览器内 OAuth 身份验证。这意味着您可以在 GitHub 和其他平台上启用双因素身份验证,从而大大提高您的安全性账户。

当您推送时,您可以选择身份验证方法:

> git push
Select an authentication method for 'https://github.com/':
  1. Web browser (default)
  2. Device code
  3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...

在 Linux 上,需要进行少量设置。以下将凭据在内存中缓存 20 小时,因此您每天最多必须进行一次身份验证。

git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"

熟悉 gnome-keyring 或 KWallet 的高级用户可能更喜欢 将凭证存储更改为 libsecret。

外观配置 (docs< /a>):

  1. 更喜欢在终端而不是 GUI 中选择身份验证方法(点击次数更少)
  2. 始终使用浏览器方法而不是每次都被询问(甚至更少的按键次数)
git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser

As of 2021, there is a secure user-friendly cross-platform solution for HTTPS remotes. No more typing passwords! No more SSH keys! No more personal access tokens!

Install Git Credential Manager developed by GitHub (downloads). It supports passwordless in-browser OAuth authentication to GitHub, BitBucket, Azure and GitLab. This means you can enable two-factor authentication on GitHub and the other platforms, greatly improving the security of your accounts.

When you push, you are offered a choice of authentication methods:

> git push
Select an authentication method for 'https://github.com/':
  1. Web browser (default)
  2. Device code
  3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...

On Linux, a tiny bit of setup is required. The following caches credentials in memory for 20 hours, so you have to authenticate at most once per day.

git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"

Power users familiar with gnome-keyring or KWallet may prefer to change the credential store to libsecret.

Cosmetic configuration (docs):

  1. Prefer choosing authentication method at terminal rather than in GUI (fewer clicks)
  2. Always use browser method rather than be asked every time (even fewer keypresses)
git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser
笑,眼淚并存 2024-11-25 01:42:35

如果您在 Windows 下使用 Git(例如 Git Bash)
(如果您不想从 HTTPS 切换到 SSH),您也可以使用 Windows 版 Git Credential Manager

此应用程序将为您保留用户名和密码...

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

国粹 2024-11-25 01:42:35

块引用

如果您遇到 $ git Push 代码问题,要求输入用户名 && , 密码请按照以下步骤操作:

Login to your profile.
Go to settings

click developer settings
->Personal access tokens
--> note: access token
--->Select scopes: checkbox on repo
----> Generate token
-----> Make sure to copy and store it safely if you intend to reuse it.

*** use the generated token as password during pushing code for next 30days ***

***HAPPY CODING***

Blockquote

if you experience $ git push code issues requesting for username && password follow the procedure below:

Login to your profile.
Go to settings

click developer settings
->Personal access tokens
--> note: access token
--->Select scopes: checkbox on repo
----> Generate token
-----> Make sure to copy and store it safely if you intend to reuse it.

*** use the generated token as password during pushing code for next 30days ***

***HAPPY CODING***
北恋 2024-11-25 01:42:35

正如许多用户所说,您只需将 Git 存储库 URL 从 HTTPS 更改为 SSH 即可。

如果您尚未在计算机中生成 SSH 密钥,则必须执行此操作。

作为附加信息,进行此更改后我仍然收到相同的错误:

权限被拒绝。

就我而言,问题在于我使用 Windows Shell 执行 ngh 命令;由于此命令应打开一个提示来请求 SSH 短语,而 Windows Shell 不会打开此类提示,因此身份验证失败。

所以,我只需打开 Git shell 并在那里执行 ngh 命令,每次要求时将 SSH 短语放入提示符中,然后“瞧”......它工作得很好!

As many users has said, you just have to change your Git repository 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 these 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!

漆黑的白昼 2024-11-25 01:42:35
 # gen  the pub and priv keys
 # use "strange" naming convention, because those WILL BE more than 10 ...
 ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/[email protected]@`hostname -s`

 # set the git alias ONLY this shell session
 alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/[email protected].`hostname -s`" git'

 # who did what when and why
 git log --pretty --format='%h %ai %<(15)%ae ::: %s'

 # set the git msg
 export git_msg='issue-123 my important commit msg'

 # add all files ( danger !!! ) and commit them with the msg
 git add --all ; git commit -m "$git_msg" --author "Me <[email protected]"

 # finally 
 git push
 # gen  the pub and priv keys
 # use "strange" naming convention, because those WILL BE more than 10 ...
 ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/[email protected]@`hostname -s`

 # set the git alias ONLY this shell session
 alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/[email protected].`hostname -s`" git'

 # who did what when and why
 git log --pretty --format='%h %ai %<(15)%ae ::: %s'

 # set the git msg
 export git_msg='issue-123 my important commit msg'

 # add all files ( danger !!! ) and commit them with the msg
 git add --all ; git commit -m "$git_msg" --author "Me <[email protected]"

 # finally 
 git push
美胚控场 2024-11-25 01:42:35

你的 git 和/或 SSH 版本太旧了

我在运行 Snow Leopard 的 2010 Macbook Pro 上受到 ssh 和 git 的影响,总是提示输入密码。

最初,我无法 ssh 进入我的服务器。

我在我的服务器 (/etc/ssh/sshd_config) 上启用了 HostKeyAlgorithms +ssh-rsa,ssh-dss,我将 git 存储库和 ssh 托管到我的帐户中。通过这一更改,我能够使用用户名和密码通过 ssh 连接到 git。

作为参考,OS X 10.6.8 上 ssh 和 git 的最新版本是:

git: 1.7.5.4

ssh: OpenSSH_5.2p1

我尝试添加 DSA 密钥,但那没用。我还考虑过更新 RSA1 密钥,但看起来我的 SSH 版本太旧了。
我在 Homebrew 中查找了雪豹,但遇到了证书问题。

我的解决方案是安装 MacPorts for Snow Leopard https://github.com/macports/macports-base/releases/download/v2.8.1/MacPorts-2.8.1-10.6-SnowLeopard.pkg

我安装了 xcode 4.2 更新,可能是很难找到;不确定是否有必要。安装 MacPorts 后,运行

sudo port install git
sudo port install ssh

您可能必须启用 rsync,但输出会告诉您如何启用。

我的 git 版本现在是 2.41.0,ssh 版本是 OpenSSH_9.3p1。我重新创建了一个 4096 位 rsa 密钥并将其复制到我的服务器(和 github),现在可以使用 sshgit Push 而无需任何密码提示!

希望这对处于我位置的其他人有所帮助。

编辑:Gentoo 提到,从 OpenSSH 8.8 开始,对 RSA 1 的支持被禁用(我的服务器是 OpenSSH_9.3p1)。可以通过添加以下内容来重新启用它:

HostkeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa

如果您使用的是 PuTTY < 0.75,你就会受到这个影响。

Your git and/or SSH version is too old

I was affected by both ssh and git always prompting for passwords on my 2010 Macbook Pro running Snow Leopard.

Originally, I was unable to ssh into my server.

I had enabled HostKeyAlgorithms +ssh-rsa,ssh-dss on my server (/etc/ssh/sshd_config) which I host my git repos and ssh into my accounts. With that change, I was able to use a username and password to connect to git and through ssh.

For reference the latest versions of ssh and git on OS X 10.6.8 are:

git: 1.7.5.4

ssh: OpenSSH_5.2p1

I tried adding a DSA key, but that didn't work. I also looked into updating the RSA1 key, but it looked like my SSH version was simply too old.
I looked into Homebrew for snow leopard, but got certificate issues.

My solution is to install MacPorts for Snow Leopard https://github.com/macports/macports-base/releases/download/v2.8.1/MacPorts-2.8.1-10.6-SnowLeopard.pkg

I had the xcode 4.2 update installed which may be hard to find; not sure if that is necessary. Once MacPorts is installed, run

sudo port install git
sudo port install ssh

You may have to enable rsync, but the output will tell you how.

My versions are now 2.41.0 for git and OpenSSH_9.3p1 for ssh. I recreated a 4096 bit rsa key and copied it to my server (and to github) and can now both ssh and git push without any password prompting at all!

Hope this helps out others in my position.

Edit: Gentoo mentioned that since OpenSSH 8.8, support for RSA 1 is disabled (my server being OpenSSH_9.3p1). It can be re-enabled by adding this:

HostkeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa

If you are using PuTTY < 0.75, you will be affected by this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文