我从我的GitHub帐户克隆了一个Git存储库到我的电脑。
我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。
当我尝试使用我的电脑推送到GitHub或从GitHub中拉取时,它需要用户名和密码,但当我使用笔记本电脑时不需要!
我不想每次与origin交互时都键入用户名和密码。我在这里缺少什么?
我从我的GitHub帐户克隆了一个Git存储库到我的电脑。
我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。
当我尝试使用我的电脑推送到GitHub或从GitHub中拉取时,它需要用户名和密码,但当我使用笔记本电脑时不需要!
我不想每次与origin交互时都键入用户名和密码。我在这里缺少什么?
当前回答
# 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
其他回答
一个常见的原因是使用默认(HTTPS)而不是SSH进行克隆。您可以通过转到存储库,单击“克隆或下载”,然后单击URL字段上方的“使用SSH”按钮,然后更新源远程的URL,如下所示:
git remote set-url origin git@github.com:username/repo.git
您可以使用以下方法检查是否已将远程添加为HTTPS或SSH:
git remote -v
GitHub中记录了这一点:将远程URL从HTTPS切换到SSH。
我也有同样的问题。
所以我从项目中更改了.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
这就是对我有用的:
git remote set-url origin https://username@github.com/username/reponame.git
例子:
git remote set-url origin https://jsmith@github.com/jsmith/master.git
你基本上有两个选择。
如果您在两台计算机上使用同一用户,则需要将.pub密钥复制到PC上,这样GitHub就知道您是同一用户。
如果你已经为你的电脑创建了一个新的.pub文件,并希望将这些机器视为不同的用户,你需要在GitHub网站上注册新的.pub文件。
如果这仍然不起作用,可能是因为ssh配置不正确,ssh无法找到密钥的位置。尝试
ssh -vv username@github.com
获取SSH失败的更多信息。
如果您在Github帐户上启用了2FA,那么您的常规密码将无法用于此目的,但您可以生成一个个人访问令牌并将其替换为使用。
访问GitHub中的设置->开发人员设置->个人访问令牌页面(https://github.com/settings/tokens/new),并生成具有所有Repo权限的新令牌:
然后页面将显示新的令牌值。保存此值,并在推送到GitHub上的存储库时使用它代替密码:
> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>