简体   繁体   English

在 Windows 版 Git 中使用 GIT_SSH_COMMAND

[英]Using GIT_SSH_COMMAND in Git for Windows

I'm using Fourth release candidate of Git for Windows 2.x now, and using GIT_SSH_COMMAND in shell to avoid SSH's host verification.我现在使用 Git for Windows 2.x 的第四个候选版本,并在 shell 中使用 GIT_SSH_COMMAND 来避免 SSH 的主机验证。 In Git Bash I write something like this:在 Git Bash 中,我写了这样的东西:

$ GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git push origin master

How can I do something like this in Windows cmd?我怎样才能在 Windows cmd 中做这样的事情? Can't find any answers anywhere.在任何地方都找不到任何答案。

You don't have to set an environment variable anymore in Windows.您不必再在 Windows 中设置环境变量。

With git 2.10+ (Q3 2016), you also have the possibility to set a config for GIT_SSH_COMMAND , which is easier than an environment variable (and can be set globally, or locally for a specific repo)使用 git 2.10+(2016 年第 3 季度),您还可以为GIT_SSH_COMMAND设置配置,这比环境变量更容易(并且可以全局设置,也可以针对特定存储库在本地设置)

See commit 3c8ede3 (26 Jun 2016) by Nguyễn Thái Ngọc Duy ( pclouds ) .请参阅Nguyễn Thái Ngọc Duy ( pclouds ) 的commit 3c8ede3 (26 Jun 2016 )
(Merged by Junio C Hamano -- gitster -- in commit dc21164 , 19 Jul 2016) (由Junio C gitster合并-- gitster -- in commit dc21164 ,2016 年 7 月 19 日)

A new configuration variable core.sshCommand has been added to specify what value for GIT_SSH_COMMAND to use per repository.添加了新的配置变量core.sshCommand以指定每个存储库使用的 GIT_SSH_COMMAND 值。

core.sshCommand:

If this variable is set, git fetch and git push will use the specified command instead of ssh when they need to connect to a remote system.如果设置了这个变量, git fetchgit push将在需要连接到远程系统时使用指定的命令而不是ssh
The command is in the same form as the GIT_SSH_COMMAND environment variable and is overridden when the environment variable is set.该命令与GIT_SSH_COMMAND环境变量的形式相同,并在GIT_SSH_COMMAND环境变量时被覆盖。

It means the git push can be:这意味着git push可以是:

cd /path/to/my/repo
git config core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' 
# later on
git push origin master

With Git 2.16 (Q1 2018), you will have a new mechanism to upgrade the wire protocol in place is proposed and demonstrated that it works with the older versions of Git without harming them.使用 Git 2.16(2018 年第一季度),您将拥有一种新机制来升级有线协议,并证明它可以与旧版本的 Git 一起使用而不会损害它们。

See commit 6464679 (16 Oct 2017), and commit 0cd8328 (26 Sep 2017) by Jonathan Tan ( jhowtan ) .犯6464679 (2017年10月16日),并提交0cd8328通过(2017年9月26日)乔纳森·谭( jhowtan
See commit 94b8ae5 , commit 3c88ebd , commit 19113a2 , commit 0c2f0d2 , commit 2609043 , commit aa9bab2 , commit dfe422d , commit 373d70e , commit 5d2124b (16 Oct 2017) by Brandon Williams ( mbrandonw ) .参见commit 94b8ae5commit 3c88ebdcommit 19113a2commit 0c2f0d2commit 2609043commit aa9bab2commit dfe422dcommit 373d70ecommit 5d2124b (16) Brandon 威廉姆斯 ( mbrandonw )
(Merged by Junio C Hamano -- gitster -- in commit 4c6dad0 , 06 Dec 2017) (由Junio C gitster合并-- gitster -- in commit 4c6dad0 ,2017 年 12 月 6 日)

ssh : introduce a ' simple ' ssh variant ssh : 引入一个“ simple ”的 ssh 变体

When using the ' ssh ' transport, the ' -o ' option is used to specify an environment variable which should be set on the remote end.使用“ ssh ”传输时,“- -o ”选项用于指定应在远程端设置的环境变量。
This allows Git to send additional information when contacting the server, requesting the use of a different protocol version via the ' GIT_PROTOCOL ' environment variable like so: " -o SendEnv=GIT_PROTOCOL ".这允许 Git 在联系服务器时发送附加信息,通过“ GIT_PROTOCOL ”环境变量请求使用不同的协议版本,如下所示:“ -o SendEnv=GIT_PROTOCOL ”。

Unfortunately not all ssh variants support the sending of environment variables to the remote end.不幸的是,并非所有 ssh 变体都支持将环境变量发送到远程端。
To account for this, only use the ' -o ' option for ssh variants which are OpenSSH compliant.为了解决这个问题,只对符合 OpenSSH 的 ssh 变体使用“ -o ”选项。
This is done by checking that the basename of the ssh command is ' ssh ' or the ssh variant is overridden to be ' ssh ' (via the ssh.variant config).这是通过检查 ssh 命令的基本名称是否为“ ssh ”或将 ssh 变体覆盖为“ ssh ”(通过ssh.variant配置)来完成的。

Other options like ' -p ' and ' -P ', which are used to specify a specific port to use, or ' -4 ' and ' -6 ', which are used to indicate that IPV4 or IPV6 addresses should be used, may also not be supported by all ssh variants.其他选项,如“ -p ”和“ -P ”,用于指定要使用的特定端口,或“ -4 ”和“ -6 ”,用于指示应使用 IPV4 或 IPV6 地址,可能也并非所有 ssh 变体都支持。

Currently if an ssh command's basename wasn't ' plink ' or ' tortoiseplink ', Git assumes that the command is an OpenSSH variant.当前,如果 ssh 命令的基本名称不是“ plink ”或“ tortoiseplink ”,Git 会假定该命令是 OpenSSH 变体。
Since user configured ssh commands may not be OpenSSH compliant, tighten this constraint and assume a variant of ' simple ' if the basename of the command doesn't match the variants known to Git.由于用户配置的 ssh 命令可能与 OpenSSH 不兼容,因此如果命令的基本名称与 Git 已知的变体不匹配,请收紧此约束并假定为“ simple ”的变体。
The new ssh variant ' simple ' will only have the host and command to execute ( [username@]host command) passed as parameters to the ssh command.新的 ssh 变体“ simple ”将仅将主机和要执行的命令( [username@]host命令)作为参数传递给ssh命令。

这是答案:

set GIT_SSH_COMMAND=ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no & git push origin master

There's a better way to achieve your goal .更好的方法来实现您的目标 While the question and various answers are interesting and instructive, the best way to achieve the goal does not involve the GIT_SSH_COMMAND variable.虽然问题和各种答案很有趣且具有启发性,但实现目标的最佳方法不涉及 GIT_SSH_COMMAND 变量。

Instead of putting the information in "git", put the information into "ssh".不是将信息放入“git”,而是将信息放入“ssh”。

The "ssh" command has always had a "config" file in Unix/Linux. “ssh”命令在 Unix/Linux 中总是有一个“config”文件。 That capability is available in Windows, also, at least in recent years.该功能在 Windows 中也可用,至少在最近几年是这样。 Configure "ssh" to know about your identity file, and then when you go to perform the "git clone" command...配置“ssh”以了解您的身份文件,然后当您执行“git clone”命令时...

git clone <repository>

...specify the <repository> in the style of ssh protocol. ...以ssh协议的风格指定<repository> Here is the ssh protocol style, as described on the "git" reference page for "git clone", at the sub-header "GIT URLs" :这是 ssh 协议样式,如“git clone”的“git”参考页面中所述,位于子标题“GIT URLs”

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

Note that there is no scheme specifier;请注意,没有方案说明符; which is to say there is no indication of ssh: nor git: nor https: nor ftps: .也就是说,没有ssh:git:https:ftps:指示。 Instead, the colon : is separating "host" from "path".相反,冒号:将“主机”与“路径”分开。 This use of the colon : does not conform to URL format, and the non-conformance distinguishes this protocol style from the others.这种冒号:使用不符合 URL 格式,并且不符合性将这种协议风格与其他风格区分开来。

A potentially important benefit of doing the configuration within "ssh" is that "ssh" allows alias names.在“ssh”中进行配置的一个潜在的重要好处是“ssh”允许别名。 Suppose, for "ssh" sessions to a particular machine, I normally want to login as myself, but sometimes I want to login as somebody else, named "user_for_git".假设,对于特定机器的“ssh”会话,我通常想以自己的身份登录,但有时我想以名为“user_for_git”的其他人身份登录。 Further suppose that the identity files for the users are different.进一步假设用户的身份文件是不同的。 The following "config" for "ssh" has entries that are named by the real domain for myself, but with an alias for the "user_for_git".以下“ssh”的“配置”具有由真实域为我自己命名的条目,但具有“user_for_git”的别名。 Note that the keyword "Host" introduces a section, while the variable "HostName" is one of several parameters within the section.请注意,关键字“Host”引入了一个部分,而变量“HostName”是该部分中的几个参数之一。

Host go_git.example.com
    # Specify the real host name
    HostName    = example.com
    User        = user_for_git
    IdentityFile    = ~/.ssh/id_rsa_for_git_at_example_com

Host example.com
    # Specify the real host name
    HostName    = example.com
    User        = user_me
    IdentityFile    = ~/.ssh/id_rsa_for_normal

With that in place, the "git clone" command can be as follows:有了这个,“git clone”命令可以如下:

git clone go_git.example.host:/path/to/repo.git

Note that in this case the optional user@ portion of the syntax is not used.请注意,在这种情况下,不使用语法的可选user@部分。 The remote repository is specified by just host : path , but in this case the host is an alias defined in the "ssh" config file.远程存储库仅由host : path指定,但在这种情况下, host是“ssh”配置文件中定义的别名。

In Linux/Unix, the "ssh" config file is named config and is in the hidden directory .ssh , which is located under the home directory $HOME ;在 Linux/Unix 中,“ssh”配置文件名为config并位于隐藏目录.ssh ,该目录位于主目录$HOME this is expressed as ~/.ssh/config .这表示为~/.ssh/config For the equivalent location(s) within Windows, check the answers here: https://stackoverflow.com/a/62842368/3552393对于 Windows 中的等效位置,请在此处查看答案: https : //stackoverflow.com/a/62842368/3552393

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM