运行“git clone [email protected]”时如何提供用户名和密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10054318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I provide a username and password when running "git clone [email protected]"?
提问by coordinate
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 [email protected]
I've tried like this:
我试过这样:
git clone username:password@[email protected]
git clone git@username:[email protected]
git clone [email protected]@username:password
But they haven't worked.
但他们没有工作。
回答by Bassetassen
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://[email protected]/username/repository.git
It will prompt you for your password.
它会提示您输入密码。
Alternatively, you may use:
或者,您可以使用:
git clone https://username:[email protected]/username/repository.git
This way worked for me from a GitHub repository.
这种方式从 GitHub 存储库对我有用。
回答by Richard Hansen
The user@host:path/to/repo
format tells Git to use ssh to log in to host
with username user
. From git help clone
:
该user@host:path/to/repo
格式告诉 Git 使用 ssh 以host
username 登录user
。来自git help clone
:
An alternative scp-like syntax may also be used with the ssh protocol:
[user@]host.xz:path/to/repo.git/
另一种类似 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. 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.
前面的部分@
是用户名,认证方式(密码、公钥等)由ssh决定,而不是Git。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
fileon the remote server, and load your private key into 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 密码,典型的解决方案是生成一个公钥/私钥对,将公钥放在远程服务器上的~/.ssh/authorized_keys
文件中,然后将您的私钥加载到ssh-agent
. 另请参阅通过 SSH 配置 Git 以登录一次、GitHub 的 ssh 密钥密码帮助页面、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). To pick which account you want to log in as, you have to tell ssh which private key to use.
如果您在 GitHub 或 Heroku 等地方拥有多个帐户,则您将拥有多个 ssh 密钥(每个帐户至少一个)。要选择您想要登录的帐户,您必须告诉 ssh 要使用哪个私钥。
For example, suppose you had two GitHub accounts: foo
and bar
. Your ssh key for foo
is ~/.ssh/foo_github_id
and your ssh key for bar
is ~/.ssh/bar_github_id
. You want to access [email protected]:foo/foo.git
with your foo
account and [email protected]:bar/bar.git
with your bar
account. You would add the following to your ~/.ssh/config
:
例如,假设您有两个 GitHub 帐户: foo
和bar
. 您的 ssh 密钥foo
是~/.ssh/foo_github_id
,您的 ssh 密钥bar
是~/.ssh/bar_github_id
。您想[email protected]:foo/foo.git
使用您的foo
帐户和[email protected]: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
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:
https://username:[email protected]/username/repository.git
Gitorious:
https://username:[email protected]/project/repository.git
Heroku: See this support article.
GitHub:
https://username:[email protected]/username/repository.git
巨大的:
https://username:[email protected]/project/repository.git
Heroku:请参阅此支持文章。
WARNING: Adding your password to the clone URL will cause Git to store your plaintext password in .git/config
. To securely store your password when using HTTP, use a credential helper. For example:
警告:将您的密码添加到克隆 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
The above will cause Git to ask for your password once every 15 minutes (by default). See git help credentials
for details.
以上将导致 Git 每 15 分钟(默认情况下)要求您输入一次密码。详情请参阅git help credentials
。
回答by Rob Rose
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. 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.
在@Bassetassen 的回答的评论中,@plosco提到您git clone https://<token>@github.com/username/repository.git
至少可以使用从 GitHub 进行克隆。我想我会扩展如何做到这一点,以防万一有人遇到这个答案,就像我在尝试自动化一些克隆时所做的那样。
GitHub has a very handyguide 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. It warns that adding the token to the clone URL will store it in plaintext in.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.
GitHub 有一个关于如何执行此操作的非常方便的指南,但它没有涵盖如果您想将其全部包含在一行中以用于自动化目的该怎么做。它警告将令牌添加到克隆 URL 会将其以纯文本形式存储在.git/config
. 对于几乎每个用例来说,这显然都是一个安全风险,但由于我计划在完成后删除 repo 并撤销令牌,所以我不在乎。
1. Create a Token
1. 创建令牌
GitHub has a whole guide hereon 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. (here'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.
- Copy your new token before you leave the page
- 转到设置 > 开发人员设置 > 个人访问令牌(这是一个直接链接)
- 单击“生成新令牌”并再次输入您的密码。(这是另一个直接链接)
- 为其设置描述/名称,检查“repo”权限并点击页面底部的“生成令牌”按钮。
- 在离开页面之前复制您的新令牌
2. Clone the Repo
2. 克隆 Repo
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! You can of course use .
, ..
, ~
, etc. here like you can elsewhere.
如果要将其克隆到特定文件夹,只需在末尾插入文件夹地址,如下所示:,您猜对了,要克隆到的文件夹git clone https://<token>@github.com/<username>/<repository.git> <folder>
在哪里<folder>
!您当然可以像在其他地方一样在这里使用.
、..
、~
等。
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 pageand hit the delete button next to it.
- If you don't need the repo again, delete it
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>
. - Clear your bash history to make sure the token doesn't stay logged there. There are many ways to do this, see this questionand 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.
- 如果您在一段时间内无意使用该令牌,您可能不想留下它,因此请返回令牌页面并点击它旁边的删除按钮。
- 如果您不再需要 repo,请将其删除
rm -rf <folder>
。 - 如果确实再次需要 repo,但不需要再次自动化,您可以通过执行
git remote remove origin
或仅通过运行git remote set-url origin https://github.com/<username>/<repository.git>
. - 清除您的 bash 历史记录以确保令牌不会保持记录在那里。有很多方法可以做到这一点,请参阅这个问题和这个问题。但是,在所有上述命令前面加上一个空格可能更容易,以防止它们被存储。
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.
请注意,我不是专业人士,因此上述内容可能不安全,因为任何类型的取证工作都不会留下任何痕迹。
回答by Santosh Kumar Arjunan
Though there are many answers, myself facing the repeated issue when username or password has special characters in it.
虽然有很多答案,但当用户名或密码中包含特殊字符时,我自己面临着重复的问题。
URL encode your username and password for git, then use it as part of URL itself (when there is no security concern).
URL 为 git 编码您的用户名和密码,然后将其用作 URL 本身的一部分(当没有安全问题时)。
Say, URL encoded value of username
比如说,用户名的 URL 编码值
'user+1' is user%2B1
“用户+1”是用户%2B1
and URL encoded value of password
和密码的 URL 编码值
'Welcome@1234' is Welcome%401234
“欢迎@1234”欢迎%401234
Then your GIT Clone URL would look like,
然后你的 GIT Clone URL 看起来像,
git clone https://user%2B1:Welcome%401234@actual-git-url-for-the-repo
works perfectly, whereas,git clone https://user+1:Welcome@1234@actual-git-url-for-the-repogives you 403 errors
git clone https://user%2B1:Welcome%401234@actual-git-url-for-the-repo
工作完美,而,git clone https://user+1:Welcome@1234@actual-git-url-for-the-repo给你 403 错误
Hope this helps.
希望这可以帮助。
Just in case, want to URL encode online: https://www.urlencoder.org/
以防万一,想在线进行 URL 编码:https: //www.urlencoder.org/
回答by Ore4444
On Windows, the following steps should re-trigger the GitHub login window when git clone
ing:
在 Windows 上,以下步骤应该在git clone
ing时重新触发 GitHub 登录窗口:
- Search start menu for "Credential Manager"
- Select "Windows Credentials"
- Delete any credentials related to Git or GitHub
- 搜索“凭据管理器”的开始菜单
- 选择“Windows 凭据”
- 删除与 Git 或 GitHub 相关的任何凭据
回答by Anshuman Bardhan
Follow this arguments to replace with ur special cases if they are creating an issue:
如果它们造成问题,请按照以下参数替换为您的特殊情况:
! # $ & ' ( ) * + , / : ; = ? @ [ ]
!# $ & ' ( ) * + , / : ; = ? @ [ ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
So for example-
所以例如 -
actual URL: https://usern@me:p@ssword@git/reponame.git
实际网址:https://usern@me:p@ssword@git/reponame.git
Solution URL to use: https://usern%40me:p%40ssword@git/reponame.git
要使用的解决方案 URL:https://usern%40me:p%40ssword@git/reponame.git
回答by Rohan
git config --global core.askpass
Run this first before cloning the same way, should be fixed!
在以相同方式克隆之前先运行它,应该修复!
回答by Michael M
This is an excellent Stack Overflow question, and the answers have been very instructive, such that I was able to resolve an annoying problem I ran into recently.
这是一个很棒的 Stack Overflow 问题,答案非常有启发性,以至于我能够解决我最近遇到的一个烦人的问题。
The organization that I work for uses Atlassian's BitBucket
product (not Github), essentially their version of GitHub so that repositories can be secured completely on premise. I was running into a similar problem as @coordinate, in that my password was required for a new repository I checked out. My credentials had been saved globally for all BitBucket
projects, so I'm not sure what prompted the loss of credentials.
我工作的组织使用Atlassian's BitBucket
产品(不是 Github),本质上是他们的 GitHub 版本,以便可以在内部完全保护存储库。我遇到了与@coordinate 类似的问题,因为我检出的新存储库需要我的密码。我的凭据已为所有BitBucket
项目全局保存,因此我不确定是什么原因导致凭据丢失。
In short, I was able to enter the following GIT command (supplying onlymy username), which then prompted Git's Credential Manager to prompt me for the password, which I was then able to save.
简而言之,我能够输入以下 GIT 命令(仅提供我的用户名),然后提示 Git 的凭据管理器提示我输入密码,然后我就可以保存密码。
git clone https://[email protected]/git/[organization]/[team]/[repository.git]
NOTE: the bracketed directory sub-paths simply refer to internal references, and will vary for you!
注意:括号内的目录子路径仅指内部参考,并且会因您而异!
回答by Rakib
If you're using http/https
and you're looking to FULLY AUTOMATEthe process without requiring any user input or any user prompt at all (for example: inside a CI/CD pipeline), you may use the following approach leveraging git credential.helper
如果您正在使用http/https
并且希望完全自动化该过程而不需要任何用户输入或任何用户提示(例如:在 CI/CD 管道内),您可以使用以下方法利用git credential.helper
GIT_CREDS_PATH="/my/random/path/to/a/git/creds/file"
# Or you may choose to not specify GIT_CREDS_PATH at all.
# See https://git-scm.com/docs/git-credential-store#FILES for the defaults used
git config --global credential.helper "store --file ${GIT_CREDS_PATH}"
echo "https://alice:${ALICE_GITHUB_PASSWORD}@github.com" > ${GIT_CREDS_PATH}
where you may choose to set the ALICE_GITHUB_PASSWORD
environment variable from a previous shell command or from your pipeline config etc.
您可以选择ALICE_GITHUB_PASSWORD
从以前的 shell 命令或管道配置等设置环境变量。
Remember that "store" based git-credential-helper stores passwords & values in plain-text. So make sure your token/password has very limited permissions.
请记住,基于“存储”的 git-credential-helper 以纯文本形式存储密码和值。因此,请确保您的令牌/密码具有非常有限的权限。
Now simply use https://[email protected]/my_repo.gitwherever your automated system needs to fetch the repo - it will use the credentials for alice
in github.com
as store by git-credential-helper.
现在只需使用HTTPS://[email protected]/my_repo.git无论您的自动化系统需要获取回购-它将使用的凭据alice
在github.com
通过GIT-凭证辅助的商店。