git.cmd vs git.exe - what is the difference and which one should be used?

prusswan picture prusswan · Jan 20, 2012 · Viewed 20.7k times · Source

I have a rough idea that git.cmd is only a wrapper (but added to PATH by default), but I found out that git.exe works as well and I intend to use it as a workaround to this issue (comments to it rather, regarding chcp on XP64). Would that be not recommended for any reason at all? Also, is git.cmd really needed in the first place?

Note: The chcp issue I am referring to is not caused by missing PATH entries as in 'chcp' is not recognized as an internal or external command, operable program or batch file. on a Windows PC

Answer

djs picture djs · Jan 22, 2013

git.cmd no longer exists in current versions of msysgit (e.g. 1.8.0). git.cmd was a wrapper that has been replaced by a new wrapped called git.exe. This is not to be confused with the actual git.exe.

If you take a look at the Git directory in %ProgramFiles(x86)% or %ProgramFiles%, you will see the following structure:

Git
|-- bin
|   |-- git.exe
|-- cmd
    |-- git.exe

The wrapper has existed in msysgit for a long time in order to properly set up the environment for using git from cmd.exe. If you are using the included bash shell, it will run git.exe directly.

You can compare the old cmd version with the new executable wrapper here:

  1. git.cmd
  2. git.exe wrapper

You don't really need to worry about any of this magic, just understand that you should call the wrapper from anything but the msysgit bash environment. When you add git to the path in the installer, it's the Git\cmd directory that is added. I don't recommend ever adding all the included utilities to your system path, as this can cause a lot of problems, especially if you have other msys or cygwin installations. I've never actually tried it in recent memory, but I would imagine it puts both the cmd and bin directories in your path, with cmd taking precedence.

For me, there is one huge advantage to the new git.exe wrapper: it makes code that calls git more portable. Previously, if I wrote a python script that called git, I would have to either execute the command with a shell environment (subprocess.Popen() with shell=True) or run the cmd file explicitly. Now, I can just execute a process with 'git' as the name, regardless of the OS. This is because CreateProcess() on Windows will not execute a batch file (.cmd is an alias for .bat), you need to invoke cmd.exe to execute it.