51

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.

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.

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?)

ygoe
  • 18,655
  • 23
  • 113
  • 210

7 Answers7

79

If you are inside of (or if you can open) your git bash shell, you can use 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).

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • 11
    @user3105700 Uh, no, this works in Windows (see my result line). I'm using the MinGW bash that comes with Git for Windows. – leonbloy May 18 '14 at 21:33
  • 2
    Thanks! Found that SourceTree installs git at `C:\Users\\AppData\Local\Atlassian\SourceTree\git_local\bin`. Just put it in `PATH`, and `cmd` now recognizes `git`. Again, thank you! – falsarella Jun 13 '15 at 03:45
19

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

@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.

Edit: Git for Windows 2.6.1 now additionally writes the CurrentVersion, InstallPath and LibexecPath values to the HKEY_LOCAL_MACHINE\Software\GitForWindows key.

sschuberth
  • 28,386
  • 6
  • 101
  • 146
  • 1
    Meanwhile, I have implemented it like that: Search for %ProgramFiles%\Git*\bin\git.exe. If it's not there, retry in %ProgramFiles(x86)%. What do you think about that? – ygoe Jan 06 '12 at 22:08
  • That should be fine if it's not taking too long. I guess the complete steps should be: 1. Check if git.exe is in PATH. 2. Otherwise, check the Registry's "InstallLocation" key. 3. Otherwise, check Git\bin\git.exe under %ProgramFiles% (or under %ProgramFiles (x86)% for 64-bit Windows). – sschuberth Jan 09 '12 at 09:40
  • Shouldn't the set PATH=%GIT%bin;%PATH% line be commented above to not run with unintended results? – Law Jan 10 '16 at 16:20
  • @law True, I've posted an improved version that I'm using since a while which fixes that and also looks at HKCU. – sschuberth Jan 10 '16 at 16:27
  • another folder git might be installed is `%userprofile%\AppData\Local\Programs\Git` – emrahgunduz Nov 30 '16 at 15:50
  • When installed in userprofile, the InstallPath key is found in HKEY_CURRENT_USER\Software\GitForWindows – tardyp Dec 19 '18 at 16:22
11

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

  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

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

Hope it helps

Sukalpo
  • 326
  • 3
  • 12
8

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

1

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

James Skemp
  • 8,018
  • 9
  • 64
  • 107
0

Ancient question, but if you're installing git through through the standard installer, one way to get where its installed is by asking the registry:

In powershell:

(Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\GitForWindows).InstallPath

This returns:

C:\Program Files\Git
Red 5
  • 318
  • 3
  • 13
0

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

Asher
  • 1,867
  • 15
  • 24