git push 命令中的用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29776439/
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
Username and password in command for git push
提问by Jake
It's possible to clone down a git repository, specifying username and password in the command. Example:
可以克隆一个 git 存储库,在命令中指定用户名和密码。例子:
git clone https://username:[email protected]/file.git
git clone https://username:[email protected]/file.git
Is it is possible to also specify the username and password when pushing? So that, for example, running git push origin --all
will then output asking for a password. I want this in one command.
推送时是否也可以指定用户名和密码?因此,例如,运行git push origin --all
将输出要求输入密码。我希望在一个命令中执行此操作。
(I am aware of the ability to set up keys and other solutions, but I want to know if there is a way to just keep using username and password with one command.) I am running Git Bash on Windows 8.1.
(我知道设置密钥和其他解决方案的能力,但我想知道是否有一种方法可以通过一个命令继续使用用户名和密码。)我在 Windows 8.1 上运行 Git Bash。
回答by number5
Yes, you can do
是的,你可以
git push https://username:[email protected]/file.git --all
git push https://username:[email protected]/file.git --all
in this case https://username:[email protected]/file.git
replace the origin
in git push origin --all
在这种情况下https://username:[email protected]/file.git
替换origin
ingit push origin --all
To see more options for git push
, try git help push
要查看更多选项git push
,请尝试git help push
回答by Hamidreza
I used below format
我使用以下格式
git push https://username:[email protected]/file.git--all
git push https://username:[email protected]/file.git--all
and if your password or username contain @ replace it with %40
如果您的密码或用户名包含@,请将其替换为 %40
回答by poke
Git will not store the password when you use URLs like that. Instead, it will just store the username, so it only needs to prompt you for the password the next time. As explained in the manual, to store the password, you should use an external credential helper. For Windows, you can use the Windows Credential Store for Git. This helper is also included by default in GitHub for Windows.
当您使用这样的 URL 时,Git 不会存储密码。相反,它只会存储用户名,因此下次只需提示您输入密码。如手册中所述,要存储密码,您应该使用外部凭证助手。对于 Windows,您可以使用Windows Credential Store for Git。默认情况下,此帮助程序也包含在GitHub for Windows 中。
When using it, your password will automatically be remembered, so you only need to enter it once. So when you clone, you will be asked for your password, and then every further communication with the remote will not prompt you for your password again. Instead, the credential helper will provide Git with the authentication.
使用时会自动记住您的密码,因此您只需输入一次。因此,当您克隆时,系统会要求您输入密码,然后与遥控器的每一次进一步通信都不会再次提示您输入密码。相反,凭证助手将向 Git 提供身份验证。
This of course only works for authentication via https; for ssh access ([email protected]/repository.git
) you use SSH keys and those you can remember using ssh-agent
(or PuTTY's pageant if you're using plink).
这当然仅适用于通过 https 进行身份验证;对于 ssh 访问 ( [email protected]/repository.git
),您使用 SSH 密钥和那些您记得使用的密钥ssh-agent
(或者 PuTTY 的选美,如果您使用的是 plink)。
回答by Alexey Orlenko
According to the Git documentation, the last argument of the git push
command can be the repository that you want to push to:
根据Git 文档,该git push
命令的最后一个参数可以是您要推送到的存储库:
git push [--all | --mirror | --tags] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
[--repo=<repository>] [-f | --force] [--prune] [-v | --verbose] [-u | --set-upstream]
[<repository> [<refspec>…]]
And the repository
parameter can be either a URLor a remote name.
并且repository
参数可以是URL或远程名称。
So you can specify username and password the same way as you do in your example of clone
command.
因此,您可以像在clone
命令示例中一样指定用户名和密码。
回答by VonC
It is possible but, before git 2.9.3 (august 2016), a git push
would print the full url used when pushing back to the cloned repo.
That would include your username and password!
这是可能的,但在 git 2.9.3(2016 年 8 月)之前,agit push
将打印推回克隆的 repo 时使用的完整 url。
这将包括您的用户名和密码!
But no more: See commit 68f3c07(20 Jul 2016), and commit 882d49c(14 Jul 2016) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
--in commit 71076e1, 08 Aug 2016)
但是,没有更多:见犯68f3c07(2016年7月20日),并提交882d49c通过(2016年7月14日),杰夫·金(peff
)。
(由Junio C gitster
Hamano合并-- --在commit 71076e1,2016 年 8 月 8 日)
push
: anonymize URL in status outputCommit 47abd85 (fetch: Strip usernames from url's before storing them, 2009-04-17, Git 1.6.4)taught fetch to anonymize URLs.
The primary purpose there was to avoid sticking passwords in merge-commit messages, but as a side effect, we also avoid printing them to stderr.The push side does not have the merge-commit problem, but it probably should avoid printing them to stderr. We can reuse the same anonymizing function.
Note that for this to come up, the credentials would have to appear either on the command line or in a git config file, neither of which is particularly secure.
So people shouldbe switching to using credential helpers instead, which makes this problem go away.But that's no excuse not to improve the situation for people who for whatever reason end up using credentials embedded in the URL.
push
: 在状态输出中匿名化 URL提交 47abd85(获取:在存储之前从 url 中剥离用户名,2009-04-17,Git 1.6.4)教 fetch 匿名化 URL。
主要目的是避免在合并提交消息中粘贴密码,但作为副作用,我们也避免将它们打印到 stderr。推送方没有合并提交问题,但它可能应该避免将它们打印到 stderr。我们可以重用相同的匿名化功能。
请注意,要出现这种情况,凭据必须出现在命令行或 git 配置文件中,这两者都不是特别安全。
所以人们应该转而使用凭证助手,这会使这个问题消失。但这并不是不改善出于某种原因最终使用嵌入在 URL 中的凭据的人的情况的借口。
回答by Chris Adams
For anyone having issues with passwords with special chars just omit the password and it will prompt you for it:
对于任何有特殊字符密码问题的人,只需省略密码,它会提示您输入:
git push https://[email protected]/YOUR_GIT_USERNAME/yourGitFileName.git