如何在运行时提供用户名和密码;git克隆git@remote.git&引用;?

如何在运行时提供用户名和密码;git克隆git@remote.git&引用;?,git,Git,我知道如何为HTTPS请求提供用户名和密码,如下所示: git clone https://username:password@remote git clone git@remote.git git clone username:password@git@remote.git git clone git@username:password@remote.git git clone git@remote.git@username:password 但我想知道如何向远程设备提供用户名和密码,如

我知道如何为HTTPS请求提供用户名和密码,如下所示:

git clone https://username:password@remote
git clone git@remote.git
git clone username:password@git@remote.git
git clone git@username:password@remote.git
git clone git@remote.git@username:password
但我想知道如何向远程设备提供用户名和密码,如下所示:

git clone https://username:password@remote
git clone git@remote.git
git clone username:password@git@remote.git
git clone git@username:password@remote.git
git clone git@remote.git@username:password
我试过这样做:

git clone https://username:password@remote
git clone git@remote.git
git clone username:password@git@remote.git
git clone git@username:password@remote.git
git clone git@remote.git@username:password

但它们没有起作用。

根据迈克尔·沙尔夫的评论:


您可以省去密码,这样就不会将其记录在Bash历史文件中:

git clone https://username@github.com/username/repository.git
它将提示您输入密码

或者,您可以使用:

git clone https://username:password@github.com/username/repository.git

这种方法在GitHub存储库中对我有效。

代码>user@host:path/to/repo格式告诉Git使用ssh以用户名
user
登录到
主机。发件人:

ssh协议也可以使用类似scp的替代语法:

[user@]host.xz:path/to/repo.git/

@
前面的部分是用户名,身份验证方法(密码、公钥等)由ssh而不是Git确定。Git无法向ssh传递密码,因为根据远程服务器的配置,ssh甚至可能不使用密码

使用
ssh代理
避免一直键入密码 如果您不想一直键入ssh密码,典型的解决方案是,将公钥放入远程服务器上的中,然后将私钥加载到中。另见、和

在GitHub的多个帐户(或Heroku或…)之间进行选择 如果您在GitHub或Heroku这样的地方拥有多个帐户,那么您将拥有多个ssh密钥(每个帐户至少一个)。要选择要作为哪个帐户登录,您必须

例如,假设您有两个GitHub帐户:
foo
bar
foo
的ssh密钥是
~/.ssh/foo\u github\u id
,而
bar
的ssh密钥是
~/.ssh/bar\u github\u id
。您想访问
git@github.com:foo/foo.git
与您的
foo
帐户和
git@github.com:bar/bar.git
与您的
bar
帐户。您需要将以下内容添加到
~/.ssh/config

Host gh-foo
    Hostname github.com
    User git
    IdentityFile ~/.ssh/foo_github_id
Host gh-bar
    Hostname github.com
    User git
    IdentityFile ~/.ssh/bar_github_id
然后,您将克隆这两个存储库,如下所示:

git clone gh-foo:foo/foo.git  # logs in with account foo
git clone gh-bar:bar/bar.git  # logs in with account bar
完全避免ssh 某些服务提供HTTP访问作为ssh的替代:

  • GitHub:

    https://username:password@github.com/username/repository.git
    
  • 虚假的:

    https://username:password@gitorious.org/project/repository.git
    
  • 你看

警告:将密码添加到克隆URL将导致Git将明文密码存储在
.Git/config
中。要在使用HTTP时安全地存储密码,请使用凭据帮助器。例如:

git config --global credential.helper cache
git config --global credential.https://github.com.username foo
git clone https://github.com/foo/repository.git

上述情况将导致Git每15分钟询问您的密码一次(默认情况下)。有关详细信息,请参见。

在@Bassetassen的评论中,@plosco提到,您至少可以使用
git clone https://@github.com/username/repository.git从github克隆。我想我会详细介绍如何做到这一点,以防有人会像我在尝试自动化克隆时一样遇到这个答案

GitHub有一个关于如何做到这一点的指南,但它没有涵盖如果您希望将所有内容都包含在一行中以实现自动化,该怎么做。它警告将令牌添加到克隆URL将以明文形式存储在
.git/config
中。这显然是几乎所有用例的安全风险,但因为我计划在完成后删除回购并撤销令牌,所以我不在乎

1.创建一个令牌 GitHub对如何获取令牌有一个明确的定义,但这里是TL;博士

  • 转到设置>开发人员设置>个人访问令牌()
  • 单击“生成新令牌”并再次输入密码。(h)
  • 为其设置描述/名称,检查“回购”权限并点击页面底部的“生成令牌”按钮
  • 在离开页面之前复制您的新令牌
  • 2.复制回购协议 与@plosco发出的命令相同,
    git clone https://@github.com//.git
    ,只需将
    替换为您的任何信息即可

    如果要将其克隆到特定文件夹,只需在末尾插入文件夹地址,如:
    git clone https://@github.com///code>,其中
    是,您猜到了,要将其克隆到的文件夹!当然,您可以像在其他地方一样,在这里使用
    ~
    ,等等

    3.不留痕迹 并非所有这些都是必要的,这取决于你所做的事情有多敏感

    • 如果您在一段时间内不打算使用该令牌,您可能不想将其挂起,因此请返回并点击它旁边的删除按钮
    • 如果您不再需要回购,请将其删除
      rm-rf
    • 如果您确实需要再次执行回购,但不需要再次自动执行回购,您可以通过执行
      git remote remove origin
      来删除远程,或者只需通过运行
      git remote set url origin来删除令牌https://github.com//
    • 清除bash历史记录以确保令牌不会停留在那里。有很多方法可以做到这一点,请参见和。但是,可能更容易在上面的所有命令前面加一个空格,以防止它们一开始就被存储

    请注意,我不是专业人士,因此上述内容可能不安全,因为任何类型的法医工作都不会留下任何痕迹。

    我通过以下方式解决了此问题:


    在克隆之前先运行这个方法,应该是固定的

    在Windows上,
    git clone
    ing时,以下步骤应重新触发GitHub登录窗口:

    • 搜索“凭证管理器”的开始菜单
    • 选择“Windows凭据”
    • 删除与Git或GitHub相关的任何凭据

    虽然有很多答案,但当用户名或密码在i中有特殊字符时,我自己面临着重复的问题