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

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

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

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


当前回答

来源:设置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)

其他回答

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

ssh://git@github.com/username/repo.git

而不是这样:

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

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

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

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

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

git config --list --show-origin

在我的例子中,在手动编辑配置文件“C:\Program Files\Git\mingw64\etc\gitconfig”并添加以下文本后,它就工作了。

[credential]
    helper = store

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

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

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

 # 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