Search code examples
msys2git-for-windows

Why can't the MinGW version of Git (Git for Windows) be called from MSYS2 Bash?


I'm currently learning how to use Linux utilities as part of my workflow via MSYS2 on Windows OS, and I learned (if my understanding is correct) that the Git which comes with MSYS2 is compiled with Cygwin, and therefore calls POSIX APIs as it runs, which MSYS2 has to emulate. This causes the Git that comes with MSYS2 to run "quite a bit slower", according to the MSYS2 website. I also found people on Reddit complaining that it's slow.

Meanwhile, the Git that comes with Git for Windows is modified (?) to only need the Win32 API and compile with MinGW, so it's faster as it doesn't need the POSIX emulation. I did some more research and found that the Git for Windows team wrote a guide teaching you how to install their MinGW version of Git inside MSYS2. But they also put a big warning right at the top of the page stating that this is not a supported method of using Git for Windows and might cause bugs.

Now the part I don't understand is, why does the shell that you're calling the executable from matter if it's already compiled? My understanding is that if you compile a program with MinGW, then it would always only have access to Win32 APIs no matter if you call it from Command Prompt or MSYS2 Bash, and if you compile a program with the GCC provided inside MSYS2, then it would always have access to POSIX APIs no matter where you call it from. So seeing as the executable provided by "Git for Windows" is already compiled, why can't I just simply call the Git for Windows executable directly instead of "installing" it in MSYS2 Bash (which is not recommended according to the Git for Windows team)? What confuses me even more is that the Git for Windows executable is clearly designed to work in both CMD/PowerShell and Bash, since Git for Windows installs "Git CMD" and "Git Bash" for you to choose one to use git in (I guess the executable somehow detects what shell it is running in and adjusts output line-endings accordingly?). Therefore it can't be a CR v.s. CRLF line-ending issue. Is it that the Git executable is dynamically linked to a library that it would fail to find if you called it from outside Git (CMD/Bash), or something like that?

I opened Git Bash and ran which git, and this gives me the path /mingw64/bin/git. Combined with the installation directory of Git for Windows, I assume the full path it's calling is "%PROGRAMFILES%\Git\mingw64\bin\git.exe" when I run git in Git Bash. I checked and there is indeed a git.exe at this location. So can't I simply add alias git='"${PROGRAMFILES}\\Git\\mingw64\\bin\git.exe"' to my .bashrc file and use this from MSYS2 Bash? What problems would this cause, and why?


Solution

  • why does the shell that you're calling the executable from matter if it's already compiled?

    It doesn't matter.

    why can't I just simply call the Git for Windows executable directly instead of "installing" it in MSYS2 Bash (which is not recommended according to the Git for Windows team)?

    You can, and this is what I'm doing in everyday work!

    The simple way to do it is to add Git for Windows to the (system-wide) PATH, and then launch MSYS2 shell (msys2_shell.cmd) with -full-path flag to make it not discard the system-wide PATH, but rather append it to the MSYS2 one.