How To Launch Git Bash from Windows Command Line?

WindowsGitBatch FileCmdCommand

Windows Problem Overview


I've got what I'm hoping is a simple question, but I haven't been able to find the answer yet. I would like to launch Git Bash from a Windows batch file. Here is what I tried so far:

  1. Launched Git Bash from Win 7 Start button

  2. Used CTRL+ALT+DEL to identify the process as "sh.exe"

  3. Launched sh.exe from batch file using start command

     start sh.exe
    

However, this does not launch the full Git Bash environment. Git Bash usually has "MINGW32" in the title bar, but sh.exe has a full path to ... Git\bin\sh.exe. It feels to me like there are some overlays or dependencies that I'm not aware of possibly, that also need to be loaded (pulled in? imported?).

This was one of the top results I found through searching the web, but it doesn't make complete sense to me and I'm not sure if it applies exactly to my situation:

https://stackoverflow.com/questions/6127063/running-git-from-windows-cmd-line-where-are-key-files

I'm a beginner in the world of Windows batch scripting.

Windows Solutions


Solution 1 - Windows

If you want to launch from a batch file:

  • for x86

      start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
    
  • for x64

      start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login
    

Solution 2 - Windows

I'm not sure exactly what you mean by "full Git Bash environment", but I get the nice prompt if I do

"C:\Program Files\Git\bin\sh.exe" --login

In PowerShell

& 'C:\Program Files\Git\bin\sh.exe' --login

The --login switch makes the shell execute the login shell startup files.

Solution 3 - Windows

I prefer to use git-bash.exe instead of sh.exe.

start "" "%ProgramFiles%\Git\git-bash.exe" -c "tail -f /c/Windows/win.ini"

You can stop closing the window when call /usr/bin/bash --login -i in the end;

start "" "%ProgramFiles%\Git\git-bash.exe" -c "echo 1 && echo 2 && /usr/bin/bash --login -i"

Note: I'm not sure this is a good way :)

Solution 4 - Windows

I prefer, putting git in environment variable and just calling

c:\Users\[myname]>sh
or 
c:\Users\[myname]>bash

Steps to create Environment variable (Win7)

  • From the desktop, right click the Computer icon.

  • Choose Properties from the context menu.

  • Click the Advanced system settings link.

  • Click Environment Variables.

  • In the section User variables, hit button NEW, put variable name as GIT_HOME, value as (folder-where-you-installed-git).

    • for me it is was c:\tools\git, others maybe have C:\Program Files\Git
  • find the PATH environment variable and select it. Click Edit. (If the PATH environment variable does not exist, click New).

  • In the Edit window, add a new value %GIT_HOME% and %GIT_HOME%\bin. Click OK. Close all remaining windows by clicking OK.

  • [Make sure you close the CMD which you want use for git]

  • open new Command prompt, and just type sh or bash or git-bash

Solution 5 - Windows

You can add git path to environment variables

  • For x86

> %SYSTEMDRIVE%\Program Files (x86)\Git\bin\

  • For x64

> %PROGRAMFILES%\Git\bin\

Open cmd and write this command to open git bash

sh --login

OR

bash --login

OR

sh

OR

bash

You can see this GIF image for more details:

https://media1.giphy.com/media/WSxbZkPFY490wk3abN/giphy.gif

Solution 6 - Windows

start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i

Git bash will get open.

Solution 7 - Windows

https://stackoverflow.com/a/33368029/15789

I have posted an answer here.

Open a Windows command window, and execute this script. If there is a change in your working directory, it will open a bash terminal in your working directory, and display the current git status. It keeps the bash window open, by calling exec bash.

If you have multiple projects you may create copies of this script with different project folder, and call it from a main batch script.

Solution 8 - Windows

I used the info above to help create a more permanent solution. The following will create the alias sh that you can use to open Git Bash:

echo @start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login > %systemroot%\sh.bat

Solution 9 - Windows

To access the GIT BASH with the command line.

Simply visit the Git installation directory.

In my case it was.

> C:\program\Git\bin\sh.exe

Copy and paste that path to an environment variable. enter image description here

Open command prompt and type bash

enter image description here

BOOM..! now you have successfully accessed the GIT BASH from the command prompt.

Solution 10 - Windows

The answer by Endoro has aged and I'm unable to comment;

# if you want to launch from a batch file or the command line:

start "" "%ProgramFiles%\Git\bin\sh.exe" --login

Solution 11 - Windows

Windows

Git bash default location C:\Program Files\Git\bin

So copy this folder path and paste it inside environment variables setting under system variables.

enter image description here

start -> Environment Variables

enter image description here

select Environment variable

enter image description here

Create a new environment variable like this

enter image description here

Add environment variable gtbash %gtbash% in the path variable

enter image description here

Now check by taking a new command prompt and typing sh (close already opened terminal or cmd)

enter image description here

Now live

something like this(GIF):

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric Hepperle - CodeSlayer2010View Question on Stackoverflow
Solution 1 - WindowsEndoroView Answer on Stackoverflow
Solution 2 - WindowsKlas MellbournView Answer on Stackoverflow
Solution 3 - WindowskujiyView Answer on Stackoverflow
Solution 4 - Windowsold-monkView Answer on Stackoverflow
Solution 5 - WindowsEng_FarghlyView Answer on Stackoverflow
Solution 6 - Windowsuser3616334View Answer on Stackoverflow
Solution 7 - WindowsRuntimeExceptionView Answer on Stackoverflow
Solution 8 - WindowsdpateView Answer on Stackoverflow
Solution 9 - WindowsmufazmiView Answer on Stackoverflow
Solution 10 - WindowsThaJayView Answer on Stackoverflow
Solution 11 - WindowslavaView Answer on Stackoverflow