以编程方式(非手动)查找 Windows 系统上安装 Git 的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8507368/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:00:49  来源:igfitidea点击:

Programmatically (not manually) finding the path where Git is installed on a Windows system

windowsgit

提问by ygoe

I'd like to write a small build helper tool that shall read some properties of the current Git working directory, like the last commit hash, whether there's modified files and so on. I found that it is easier to use the installed Git binaries instead of reading the .git directory with its compressed files in an unknown format. But my tools must be as portable as possible. It's intended for .NET applications, so the only requirement should be .NET 2.0 or newer.

我想写一个小的构建助手工具,它会读取当前 Git 工作目录的一些属性,比如最后一次提交的哈希值,是否有修改过的文件等等。我发现使用已安装的 Git 二进制文件比读取 .git 目录及其未知格式的压缩文件更容易。但我的工具必须尽可能便携。它适用于 .NET 应用程序,因此唯一的要求应该是 .NET 2.0 或更新版本。

Now how can I find the path where Git is installed? There's a default one that is used if the user has just clicked through the Git installer. But it may be different. And when I see all the programme files in git/bin, I really don't want that to be in my %PATH% (which other tools like TortoiseGit don't seem to require, too). I haven't found any path clues in the registry.

现在如何找到安装 Git 的路径?如果用户刚刚单击 Git 安装程序,则使用默认设置。但它可能会有所不同。当我看到 git/bin 中的所有程序文件时,我真的不希望它出现在我的 %PATH% 中(TortoiseGit 等其他工具似乎也不需要)。我在注册表中没有找到任何路径线索。

What algorithm could I use to find Git, that is not a full file system scan? (Did I already say it needs to be fast?)

我可以使用什么算法来查找 Git,这不是完整的文件系统扫描?(我已经说过它需要快吗?)

采纳答案by sschuberth

I'm using the following batch file to find out where Git for Windows has been installed:

我正在使用以下批处理文件找出 Git for Windows 的安装位置:

@echo off

setlocal enabledelayedexpansion

rem Read the Git for Windows installation path from the Registry.
for %%k in (HKCU HKLM) do (
    for %%w in (\ \Wow6432Node\) do (
        for /f "skip=2 delims=: tokens=1*" %%a in ('reg query "%%k\SOFTWARE%%wMicrosoft\Windows\CurrentVersion\Uninstall\Git_is1" /v InstallLocation 2^> nul') do (
            for /f "tokens=3" %%z in ("%%a") do (
                set GIT=%%z:%%b
                echo Found Git at "!GIT!".
                goto FOUND
            )
        )
    )
)

goto NOT_FOUND

:FOUND

rem Make sure Bash is in PATH (for running scripts).
set PATH=%GIT%bin;%PATH%

rem Do something with Git ...

:NOT_FOUND

I should be straight forward to do something similar in .NET. Just remember that you have to explicitly check the 32-bit branch of the Registry if you're on a 64-bit Windows.

我应该直接在 .NET 中做类似的事情。请记住,如果您使用的是 64 位 Windows,则必须明确检查注册表的 32 位分支。

Edit:Git for Windows 2.6.1 now additionally writesthe CurrentVersion, InstallPathand LibexecPathvalues to the HKEY_LOCAL_MACHINE\Software\GitForWindowskey.

编辑:混帐的Windows 2.6.1现在还写CurrentVersionInstallPathLibexecPath值的HKEY_LOCAL_MACHINE\Software\GitForWindows关键。

回答by leonbloy

If you are inside of (or if you can open) your git bash shell, you can use pwd -W

如果你在你的 git bash shell 里面(或者你可以打开),你可以使用 pwd -W

$ cd / && pwd -W
C:/Program Files (x86)/Git

(I know, this is probably not what you want, and it's quite elementary, but I spent some time to find this, and perhaps it's useful for other people).

(我知道,这可能不是您想要的,而且它很初级,但我花了一些时间才找到它,也许对其他人有用)。

回答by Sukalpo

If you are in Windows 8 and above here are the steps that you can follow.

如果您使用的是 Windows 8 及更高版本,这里是您可以遵循的步骤。

  1. go to your start screen and search for git.exe
  2. In the search result right click on the Git Gui/ Git Bash icon and select Open File location
  3. You will be taken to a flder where the shortcuts will be located. Right click on the shortcut nd select properties
  4. the file location can be found in the Target field
  1. 转到开始屏幕并搜索 git.exe
  2. 在搜索结果中右键单击 Git Gui/Git Bash 图标并选择打开文件位置
  3. 您将被带到快捷方式所在的文件夹。右键单击快捷方式并选择属性
  4. 文件位置可以在目标字段中找到

For me it was "C:\Users\\AppData\Local\Programs\Git\cmd\git-gui.exe"

对我来说是“C:\Users\\AppData\Local\Programs\Git\cmd\git-gui.exe”

Hope it helps

希望能帮助到你

回答by MissScarlett80

You can also open Git Bash and type where git. It will return the path where Git is installed on your machine.

您也可以打开 Git Bash 并输入where git. 它将返回 Git 安装在您机器上的路径。

回答by Tommy Williams

git --man-pathgets you to [base git installation dir]\mingw64\share\man. git.exe is at [base git installation dir]\cmd.

git --man-path让你进入 [base git 安装目录]\mingw64\share\man。git.exe 位于 [base git 安装目录]\cmd。

回答by Asher

look in the registry under: HKEY_CURRENT_USER\Software\Git-Cheetah

在注册表中查看:HKEY_CURRENT_USER\Software\Git-Cheetah