首页 文章

Git每次推送时都会询问用户名

提问于
浏览
427

每当我试图推进我的回购git要求 username & password .

我每次都重新输入密码没有问题,但问题是输入用户名 . 我使用 https 克隆我的存储库 .

那么,我如何配置git以便它不会在每个 git push 上请求 username .

我是linux的新手,但是在Windows git push 中的IIRC只要求输入密码 .

15 回答

  • 0
  • 3

    为了避免输入用户名,但仍然提示您输入密码,那么您可以简单地克隆您的存储库,包括用户名:

    git clone user_name@example.gitrepo.com/my_repo.git
    
  • 9

    编辑(由主持人和评论建议的@ dk14)

    WARNING: If you use credential.helper store from the answer, your password is going to be stored completely unencrypted ("as is") at ~/.git-credentials. Please consult the comments section below or the answers from the "Linked" section, especially if your employer has zero tolerance for security issues.

    理由:答案中描述的方法可能会引入一个主要漏洞,特别是对于运营私人财务数据的企业而言 . 结合较小的安全问题(通常存在于此)或最小的社交工程,它允许劫持项目网络中的大多数凭证,因为事先知道“裸”密码的位置 . 这可能包括部分访问 生产环境 环境的凭证,因此事实上提升了[内部]攻击者的访问权限并允许在某种程度上掩盖黑客的身份 .

    作为旁注(为了通过示例扩展此警告),尽管为员工提供了安全的虚拟环境,但几个大型企业项目似乎仍然存在这个问题......基本上我对于摆脱 https auth的呼吁在这方面遭到拒绝 - 但是,在那时候,我并不认为可以将一个漏洞利用(如 cat ~/git.credentials | curl ... )注入到Git上托管的辅助bash / build脚本中,以便将其传播到用户之间,甚至可能是匿名的(因为有些是自动同步的) . 公平地说,这种黑客通常不会(至少)直接访问关键系统 . 除非 Git-credentials === corporate-email-credentials .


    编辑#2(@ dk14):

    即使被接受,它也没有关于省略用户名(不是密码)的问题 . 对于那些确切问题的读者来说,@ grawity的answer可能会派上用场 .


    Original answer (by @Alexander Zhu):

    您可以使用以下命令存储凭据

    $ git config credential.helper store
    $ git push http://example.com/repo.git
    Username: <type your username>
    Password: <type your password>
    

    另外我建议你阅读
    $ git help credentials

  • 22

    您可以在本地存储库的 .git/config 文件中完成此操作 . 该文件包含一个名为'remote'的部分,其中包含一个名为'url'的条目 . 'url'条目应包含您正在讨论的存储库的https链接 .

    当您使用您的用户名为主机'url'作为前缀时, git 不应该是't be asking for your username anymore. Here'的示例:

    url = https://username@repository-url.com
    
  • 18

    使用Git存储库进行永久身份验证

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

    $ git config credential.helper store
    $ git push https://github.com/repo.git
    
    Username for 'https://github.com': <USERNAME>
    Password for 'https://USERNAME@github.com': <PASSWORD>
    

    使用时还应指定 caching expire

    git config --global credential.helper "cache --timeout 7200"
    

    启用凭据缓存后,将缓存 7200 seconds (2 hour) .


    阅读凭证 Docs

    $ git help credentials
    
  • 743

    如果您使用的是https而不是ssh,则可以在 .git/config 中编辑"url",如user701648所述,但如果您需要,还可以添加密码:

    url = https://username:password@repository-url.com
    
  • 1

    按照this article on GitHub中的说明添加新的SSH密钥 .

    如果Git仍然要求您输入用户名和密码,请尝试将 https://github.com/ 更改为远程URL中的 git@github.com:

    $ git config remote.origin.url 
    https://github.com/dir/repo.git
    
    $ git config remote.origin.url "git@github.com:dir/repo.git"
    
  • 75

    我找到的最简单方法是使用此命令:

    git config --global credential.https://github.com.username <your_username>
    

    这适用于逐站点并修改您的全局git配置 .

    要查看更改,请使用:

    git config --global --edit
    
  • 4

    您可以通过在Git配置文件中添加以下内容来为给定站点的所有存储库设置用户名 . 您需要将“https://example.com " and " me”更改为实际网址和实际用户名 .

    [credential "https://example.com"]
        username = me
    

    (这直接来自“git help credentials”)

  • 2

    确保使用SSH而不是http . 检查this SO回答 .

  • 21

    除了user701648的答案,您还可以使用以下命令将凭据存储在主文件夹中(所有项目的全局),而不是项目文件夹

    $ git config --global credential.helper store
    $ git push http://example.com/repo.git
    Username: <type your username>
    Password: <type your password>
    
  • 21

    更改克隆的回购工作原理:

    git remote set-url origin git@github.com:gitusername/projectname.git
    

    使用缓存凭据可以正常工作,并且设置超时时间可以减少烦恼 . 如果您使用Windows凭据帮助程序,那应该是最简单的方法(特别是在SSH被阻止的情况下) .

  • 123

    当我只是git pull,git pull origin xxx时,git要求输入用户名和密码 .

    git config credential.helper store
    

    有用 .

  • 2

    如果你使用 SSH 版本,你将不会有任何密码问题 . 只有一次生成 SSH 密钥,告诉git, this pc将与 this github帐户一起工作,从不再问我任何访问(对于这台电脑) .

    How To Generate SSH for Github

  • 5

    最简单的方法是使用以下内容创建 ~/.netrc 文件:

    machine github.com
    login YOUR_GITHUB_USERNAME
    password YOUR_GITHUB_PASSWORD
    

    (如下所示:https://gist.github.com/ahoward/2885020

    您甚至可以关闭此文件的权限,这样任何人都无法通过键入以下内容来读取您的密码:

    chmod 600 ~/.netrc
    

相关问题