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

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

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

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


当前回答

对于Mac OS

转到您的Github设置->开发者设置->Github中的个人访问令牌页面(https://github.com/settings/tokens/new),并生成具有所有Repo权限的新令牌在mac上搜索Keychain Access->搜索github.com->单击Show password,然后粘贴刚才复制的令牌。转到CLI,它将再次询问用户名和密码,输入您的Github用户名并将令牌粘贴为密码,您应该可以继续使用CLI。

其他回答

对于Mac OS

转到您的Github设置->开发者设置->Github中的个人访问令牌页面(https://github.com/settings/tokens/new),并生成具有所有Repo权限的新令牌在mac上搜索Keychain Access->搜索github.com->单击Show password,然后粘贴刚才复制的令牌。转到CLI,它将再次询问用户名和密码,输入您的Github用户名并将令牌粘贴为密码,您应该可以继续使用CLI。

使用Git存储库永久验证

运行以下命令以启用凭据缓存:

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

Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>

您还应该指定缓存过期,

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

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


正如SebastianH所评论的,也可以使用类似的命令来配置,而不是按存储库,而是全局配置:

git config --global credential.helper credential.helper store
git config --global credential.helper 'cache --timeout 7200'
 # 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

你基本上有两个选择。

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

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

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

ssh -vv username@github.com

获取SSH失败的更多信息。

列出当前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