运行“git clone git@remote.git”时如何提供用户名和密码?
I know how to provide a username and password to an HTTPS request like this: 我知道如何为HTTPS请求提供用户名和密码,
本文翻译自:How do I provide a username and password when running “git clone git@remote.git”?
I know how to provide a username and password to an HTTPS request like this: 我知道如何为HTTPS请求提供用户名和密码,如下所示:
git clone https://username:password@remote
But I'd like to know how to provide a username and password to the remote like this: 但我想知道如何为遥控器提供用户名和密码,如下所示:
git clone git@remote.git
I've tried like this: 我试过这样的:
git clone username:password@git@remote.git
git clone git@username:password@remote.git
git clone git@remote.git@username:password
But they haven't worked. 但他们没有奏效。
#1楼
参考:https://stackoom.com/question/gBaQ/运行-git-clone-git-remote-git-时如何提供用户名和密码
#2楼
Use: 使用:
git clone https://username:password@github.com/username/repository.git
This way worked for me from a GitHub repository. 这种方式对我来说是一个GitHub存储库。
Based on Michael Scharf's comment: 基于Michael Scharf的评论:
You can leave out the password so that it won't be logged in your Bash history file: 您可以省略密码,以便它不会记录在您的Bash历史记录文件中:
git clone https://username@github.com/username/repository.git
It will prompt you for your password. 它会提示您输入密码。
#3楼
The user@host:path/to/repo
format tells Git to use ssh to log in to host
with username user
. user@host:path/to/repo
格式告诉Git使用ssh登录host
用户名user
。 From git help clone
: 从git help clone
:
An alternative scp-like syntax may also be used with the ssh protocol: 另一种类似scp的语法也可以与ssh协议一起使用:
[user@]host.xz:path/to/repo.git/
The part before the @
is the username, and the authentication method (password, public key, etc.) is determined by ssh, not Git. @
之前的部分是用户名,身份验证方法(密码,公钥等)由ssh确定,而不是Git。 Git has no way to pass a password to ssh, because ssh might not even use a password depending on the configuration of the remote server. Git无法将密码传递给ssh,因为ssh甚至可能根本不使用密码,具体取决于远程服务器的配置。
Use ssh-agent
to avoid typing passwords all the time 使用ssh-agent
避免一直输入密码
If you don't want to type your ssh password all the time, the typical solution is to generate a public/private key pair , put the public key in your ~/.ssh/authorized_keys
file on the remote server, and load your private key into ssh-agent
. 如果您不想一直输入ssh密码,典型的解决方案是生成公钥/私钥对 ,将~/.ssh/authorized_keys
放在远程服务器上的~/.ssh/authorized_keys
文件中 ,然后加载私有密钥键入ssh-agent
。 Also see Configuring Git over SSH to login once , GitHub's help page on ssh key passphrases , gitolite's ssh documentation , and Heroku's ssh keys documentation . 另请参阅通过SSH配置Git以登录一次 , 关于ssh密钥密码的GitHub帮助页面 , gitolite的ssh文档和Heroku的ssh密钥文档 。
Choosing between multiple accounts at GitHub (or Heroku or...) 在GitHub(或Heroku或......)的多个帐户之间进行选择
If you have multiple accounts at a place like GitHub or Heroku, you'll have multiple ssh keys (at least one per account). 如果您在GitHub或Heroku这样的地方有多个帐户,您将拥有多个ssh密钥(每个帐户至少有一个)。 To pick which account you want to log in as, you have to tell ssh which private key to use . 要选择要登录的帐户,您必须告诉ssh使用哪个私钥 。
For example, suppose you had two GitHub accounts: foo
and bar
. 例如,假设您有两个GitHub帐户: foo
和bar
。 Your ssh key for foo
is ~/.ssh/foo_github_id
and your ssh key for bar
is ~/.ssh/bar_github_id
. 为您的SSH密钥foo
是~/.ssh/foo_github_id
并为您的SSH密钥bar
是~/.ssh/bar_github_id
。 You want to access git@github.com:foo/foo.git
with your foo
account and git@github.com:bar/bar.git
with your bar
account. 您想要使用您的foo
帐户访问git@github.com:foo/foo.git
,使用您的bar
帐户访问git@github.com:foo/foo.git
git@github.com:bar/bar.git
。 You would add the following to your ~/.ssh/config
: 您可以将以下内容添加到~/.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
You would then clone the two repositories as follows: 然后,您将克隆这两个存储库,如下所示:
git clone gh-foo:foo/foo.git # logs in with account foo
git clone gh-bar:bar/bar.git # logs in with account bar
Avoiding ssh altogether 完全避免使用ssh
Some services provide HTTP access as an alternative to ssh: 有些服务提供HTTP访问作为ssh的替代方法:
-
GitHub: GitHub的:
https://username:password@github.com/username/repository.git
-
Gitorious: Gitorious:
https://username:password@gitorious.org/project/repository.git
-
Heroku: See this support article . Heroku:请参阅此支持文章 。
WARNING : Adding your password to the clone URL will cause Git to store your plaintext password in .git/config
. 警告 :将密码添加到克隆URL将导致Git将您的纯文本密码存储在.git/config
。 To securely store your password when using HTTP, use a credential helper. 要在使用HTTP时安全地存储密码,请使用凭证帮助程序。 For example: 例如:
git config --global credential.helper cache
git config --global credential.https://github.com.username foo
git clone https://github.com/foo/repository.git
The above will cause Git to ask for your password once every 15 minutes (by default). 以上将导致Git每15分钟(默认情况下)要求输入一次密码。 See git help credentials
for details. 有关详细信息,请参阅git help credentials
#4楼
In the comments of @Bassetassen's answer , @plosco mentioned that you can use git clone https://<token>@github.com/username/repository.git
to clone from GitHub at the very least. 在@Bassetassen的回答中 ,@ plosco提到你可以使用git clone https://<token>@github.com/username/repository.git
至少从GitHub克隆。 I thought I would expand on how to do that, in case anyone comes across this answer like I did while trying to automate some cloning. 我想我会扩展如何做到这一点,以防任何人遇到这样的答案,就像我在尝试自动化克隆时所做的那样。
GitHub has a very handy guide on how to do this, but it doesn't cover what to do if you want to include it all in one line for automation purposes. GitHub有一个非常方便的指导如何执行此操作,但它不包括如果您想将它们全部包含在一行中以便进行自动化,该怎么办。 It warns that adding the token to the clone URL will store it in plaintext in .git/config
. 它警告说, 将标记添加到克隆URL将以明文形式存储在 .git/config
。 This is obviously a security risk for almost every use case, but since I plan on deleting the repo and revoking the token when I'm done, I don't care. 这显然是几乎所有用例的安全风险,但由于我计划在完成后删除回购并撤销令牌,我不在乎。
1. Create a Token 1.创建令牌
GitHub has a whole guide here on how to get a token, but here's the TL;DR. GitHub 在这里有关于如何获得令牌的完整指南 ,但这里是TL; DR。
- Go to Settings > Developer Settings > Personal Access Tokens ( here's a direct link ) 转到设置>开发人员设置>个人访问令牌 ( 这是直接链接 )
- Click "Generate a New Token" and enter your password again. 单击“生成新令牌”并再次输入密码。 (h ere's another direct link ) (这是另一个直接链接 )
- Set a description/name for it, check the "repo" permission and hit the "Generate token" button at the bottom of the page. 设置它的描述/名称,检查“repo”权限并点击页面底部的“Generate token”按钮。
- Copy your new token before you leave the page 在离开页面之前复制新令牌
2. Clone the Repo 2.克隆回购
Same as the command @plosco gave, git clone https://<token>@github.com/<username>/<repository>.git
, just replace <token>
, <username>
and <repository>
with whatever your info is. 与@plosco给出的命令相同, git clone https://<token>@github.com/<username>/<repository>.git
,只需将<token>
, <username>
和<repository>
替换为您的任何信息。
If you want to clone it to a specific folder, just insert the folder address at the end like so: git clone https://<token>@github.com/<username>/<repository.git> <folder>
, where <folder>
is, you guessed it, the folder to clone it to! 如果要将其克隆到特定文件夹,只需在末尾插入文件夹地址,如下所示: git clone https://<token>@github.com/<username>/<repository.git> <folder>
,其中,你猜对了<folder>
,将它克隆到的文件夹! You can of course use .
你当然可以使用.
, ..
, ~
, etc. here like you can elsewhere. , ..
, ~
,等等,你可以在其他地方。
3. Leave No Trace 3.不留痕迹
Not all of this may be necessary, depending on how sensitive what you're doing is. 并非所有这些都是必要的,这取决于你所做的事情的敏感程度。
- You probably don't want to leave that token hanging around if you have no intentions of using it for some time, so go back to the tokens page and hit the delete button next to it. 如果你有一段时间无意使用它,你可能不想留下那个令牌,所以回到令牌页面然后点击它旁边的删除按钮。
- If you don't need the repo again, delete it
rm -rf <folder>
. 如果您不再需要回购,请将其删除rm -rf <folder>
。 - If do need the repo again, but don't need to automate it again, you can remove the remote by doing
git remote remove origin
or just remove the token by runninggit remote set-url origin https://github.com/<username>/<repository.git>
. 如果确实需要再次使用repo,但不需要再次自动化,可以通过执行git remote remove origin
删除远程,或者只需通过运行git remote set-url origin https://github.com/<username>/<repository.git>
删除令牌git remote set-url origin https://github.com/<username>/<repository.git>
。 - Clear your bash history to make sure the token doesn't stay logged there. 清除您的bash历史记录以确保令牌不会保留在那里。 There are many ways to do this, see this question and this question . 有很多方法可以做到这一点,请看这个问题和这个问题 。 However, it may be easier to just prepend all the above commands with a space in order to prevent them being stored to begin with. 但是,可能更容易将前面的所有命令添加到空格中以防止它们被存储开始。
Note that I'm no pro, so the above may not be secure in the sense that no trace would be left for any sort of forensic work. 请注意,我不是专业人士,所以上述内容可能并不安全,因为任何法医工作都不会留下任何痕迹。
#5楼
I solved this problem in the following way: 我通过以下方式解决了这个问题:
#6楼
git config --global core.askpass
在克隆之前先运行此方法,应该修复!
更多推荐
所有评论(0)