我从我的GitHub帐户克隆了一个Git存储库到我的电脑。

我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。

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

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


当前回答

我也有同样的问题。

所以我从项目中更改了.git/config文件,

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

to

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

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

对于SSH公钥:

cat ~/.ssh/id_rsa.pub

其他回答

当您使用https进行Git推送时,只需为项目配置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://eric@github.com/eric/myproject

@更新-使用ssh

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

粗略步骤:

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

提示:

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


@更新-在https和ssh协议之间切换。

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

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

E.g.

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

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

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

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

块引用

如果您在请求用户名和密码时遇到$git推送代码问题,请执行以下步骤:

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

列出当前SSH密钥:

ls -l ~/.ssh

生成新的SSH密钥:

ssh-keygen -t ed25519 -C "your_email@example.com"

您应该更换的位置your_email@example.com使用您的GitHub电子邮件住址当提示输入保存密钥的文件时,按进来输入密码时(如果没有密码,则为空)-只需按输入(对于空密码短语)。再次列出SSH密钥:

ls -l ~/.ssh

现在应该已经添加了文件id_ed25519和id_ed25529.pub。在后台启动ssh代理:

eval $(ssh-agent -s)

将SSH私钥添加到SSH代理:

ssh-add ~/.ssh/id_ed25519

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

cat ~/.ssh/id_ed25519.pub

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


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

git status

然后cd。。到父目录和rm-fr<repo的名称>。最后克隆一个使用SSH而不是用户名/密码的新副本:

git clone git@github.com:[your-github-username]/[repository-name].git

参考文献:https://docs.github.com/en/free-pro-team@最新的/github/认证到github/生成a-new-ssh-key-and-adding-it-the-sshagenthttps://docs.github.com/en/free-pro-team@最新/github/认证到github/添加a-new-ssh-key-to-your-github-account

HTTPS更新:

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

要使用:

从此处下载程序运行程序后,它将编辑.gitconfig文件。重新检查它是否编辑了正确的.gitconfig,以防出现多个错误。如果它没有编辑正确的文件,请将以下内容添加到.gitconfig中[凭据]助手=!'C: \\Path\\To\\Your\\Downloaded\\File\\git credential winstore.exe'注意[credential]后面的换行符。这是必需的。打开命令行客户端,尝试git push origin master一次。如果它要求您输入密码,请输入密码,您就完成了。密码已保存!