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

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

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

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


当前回答

你基本上有两个选择。

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

如果你已经为你的电脑创建了一个新的.pub文件,并希望将这些机器视为不同的用户,你需要在GitHub网站上注册新的.pub文件。

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

ssh -vv username@github.com

获取SSH失败的更多信息。

其他回答

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

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

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

[user]
        name = Tux
        email = tux@gmail.com
        username = happy_feet

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

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

url = git@github.com:happy_feet/my_code.git

(以上格式不会随GitHub或Bitbucket等各种Git远程服务器而改变。如果您使用Git进行版本控制,则相同):

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

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

 # gen  the pub and priv keys
 # use "strange" naming convention, because those WILL BE more than 10 ...
 ssh-keygen -t rsa -b 4096 -C "me@corp.com" -f ~/.ssh/id_rsa.me@corp.com@`hostname -s`

 # set the git alias ONLY this shell session
 alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa.me@corp.com.`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 <me@corp.com"

 # finally 
 git push

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

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

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

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

git remote -v

它会做出如下反应

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

然后,您可以运行许多其他人建议的命令,但现在您可以从上面知道您的名字和您的repo,因此您可以将上面的name/yourrepo.git剪切并粘贴到:

git remote set-url origin git@github.com:yourname/yourrepo.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***