Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

windows

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

like image 446
ygoe Avatar asked Dec 14 '11 15:12

ygoe


People also ask

How do I find where git is installed on windows?

The default path on windows is C:\Program Files (x86)\Git . The name of the executable is not git.exe on all systems. Show activity on this post. It seems like git.exe can be found in different places depending on how it was installed, the version, and version of Windows.

How do I find my git path?

2 Answers. Git executable can be found by using running git --exec-path, which usually lives in the Git execution path. git --exec-path will give you the path.

Why git is not recognized in powershell?

If the Git path is not configured correctly, you could also receive the error “git is not recognized as an internal or external command, operable program or batch file”. You can try reinstalling Git for Windows to set the path automatically. Step 1: Open the Search utility and search for Control Panel. Then open it.

How do I know if git is installed windows?

Open the command prompt "terminal" and type git version to verify Git was installed.


2 Answers

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

like image 86
leonbloy Avatar answered Sep 24 '22 01:09

leonbloy


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.

like image 40
sschuberth Avatar answered Sep 20 '22 01:09

sschuberth