152

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:

Running git from Windows Cmd line: Where are key files?

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

Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 3
    Well, it's not *actually* the DOS command line unless: (a) You've encountered some kind of time anomaly and are using Windows 9x/ME, where `command.com` is the only shell included with the OS (b) You're on a 32-bit NT system and either invoked `command.com` yourself, or an old program did it for you. See [What is the difference between CMD and Command prompt in windows?](https://superuser.com/questions/451816/what-is-the-difference-between-cmd-and-command-prompt-in-windows) for more info. – SamB Apr 19 '19 at 17:51
  • 1
    For `batch` file this worked for me: `start "" "%PROGRAMFILES%\Git\git-bash.exe" --cd-to-home` – temo Jan 11 '22 at 11:12
  • If you want to start in a specific folder use `start "D:\MyNiceFolder" "%PROGRAMFILES%\Git\bin\sh.exe" --login`. Its even better to pin a shortcut in start menu by following [this article](https://www.easytechguides.com/pin-a-batch-file-to-the-taskbar-or-start-menu-in-windows-10/). – Rajesh Swarnkar Oct 29 '22 at 14:36

12 Answers12

165

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
    
pjpscriv
  • 866
  • 11
  • 20
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Thanks @Endoro! Though @Klas Mellbourn's answer was correct for Powershell, yours is more correct since I'm running from DOS batch file. The addition of the "start" directive causes the program to execute asynchronously, which is exactly what I need! Thumbs up! – Eric Hepperle - CodeSlayer2010 Jun 27 '13 at 15:02
  • 23
    Is there a way to type and an execute a command into the Git batch window after it's opened from within the batch file? – Nick Mar 28 '14 at 12:48
  • 2
    @OviC Are you sure your edit is correct? "Program Files (x86)" only exists under x64 architecture for 32 bit programs. – Drunix May 19 '16 at 06:51
  • 1
    @Drunix you are right, there is no "Program Files (x86)\" path in x86 systems. I did a rollback. Thanks. – Endoro Jul 01 '16 at 16:26
  • What if it's not installed on system drive? What if it's not installed in program files? – Michael Kruglos Mar 16 '17 at 18:04
  • To clarify for those confused, use the `Program Files (x86)` version only if you have a 64 bit *system*, and wish to use the 32 bit version of sh.exe. In all other cases (32 on 32 or 64 on 64), `Program Files` is correct. – R. Davidson Apr 20 '19 at 16:30
  • @Nick yes. To type a command into Git bash window you add -i -c, e.g. `start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login -i -c "the command" – ssh May 29 '21 at 10:37
68

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.

pjpscriv
  • 866
  • 11
  • 20
Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • Thanks @Klas Mellbourn. Your answer is good for Powershell, but without the addition of the "start" directive as in Endoro's answer, it halts execution of the next program until the shell is exited, which is not the desired behavior, otherwise I would have voted you up. – Eric Hepperle - CodeSlayer2010 Jun 27 '13 at 15:04
  • I use Console2 to window `cmd`, and this causes strange behavior. I put this in a .bat file and now all is well: `@"%SYSTEMDRIVE%\Program Files\Git\bin\bash.exe" --login` – Grault Sep 03 '15 at 21:36
  • 1
    This also works when inside of a Windows Docker container, thanks – Meir Gabay Oct 05 '20 at 08:48
36

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

pjpscriv
  • 866
  • 11
  • 20
kujiy
  • 5,833
  • 1
  • 29
  • 35
  • 4
    Nice answer, but the git-bash window closes immediately. Do you know how to disable auto-close after command finishes execution? – Mark Aug 09 '16 at 13:25
  • @Mark Thanks Mark. I added an example to my answer above. Unfortunately I'm not sure how we should do that in the right way because I'm not a windows person :) – kujiy Aug 12 '16 at 00:18
  • 1
    [code] start "" "%ProgramFiles%\Git\git-bash.exe" [/code] works great! – PauAI Sep 20 '16 at 08:35
  • @PauAI 2018 now... I tried `start "" "%ProgramFiles%\Git\git-bash.exe"` from an administrator command line, and though it brings up Git Bash in a new window, typing a simple command like `ls` results in no output. This method doesn't seem to allow execution of linux commands. ... EDIT: Worked, just took a long time (2 minutes) to run ls. Then ls again ran in less than 1 sec. ... weird. – Eric Hepperle - CodeSlayer2010 Aug 20 '18 at 13:06
18

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
old-monk
  • 799
  • 8
  • 20
  • 2
    Thanks! I was able to get this working with GIT_HOME as `C:\Program Files\Git`, and typing `git-bash` from CMD. Also, strangely, although you only have to type PATH to see its contents, to see contents of this git custom variable you must type `echo %GIT_HOME%` – Eric Hepperle - CodeSlayer2010 Aug 20 '18 at 13:23
  • Thank you, this was a great addition. To add to this, when you have the `GIT_HOME` variable and you use the `git-bash` command (or `git-cmd` ) it will open it in a new window. If you add to the "PATH" environment variable, it will switch in that existing window. You can also type `exit` to "logout" of the git bash and still retain the DOS prompt. – Keith E. Truesdell Jul 19 '19 at 19:00
8

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

Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34
6

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.

mufazmi
  • 1,103
  • 4
  • 18
  • 37
4
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i

Git bash will get open.

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
3

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.

Community
  • 1
  • 1
RuntimeException
  • 1,593
  • 2
  • 22
  • 31
3

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
dpate
  • 31
  • 2
  • Thanks dpate! I pasted your echo statement into the command line and ran `sh` and a git-bash window launched and I was able to run commands perfectly. Can add a little detail to your answer explaining how this works? I'd rather learn how fishing works than have somebody give me a fish, and I think it will benefit others looking for a good answer. thanks – Eric Hepperle - CodeSlayer2010 Aug 20 '18 at 13:27
2

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

lava
  • 6,020
  • 2
  • 31
  • 28
0

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
ThaJay
  • 1,758
  • 1
  • 19
  • 30
0

If you want to start a "Git Gash" window from "Git Bash" window, so "start bash" will do the work in windows 10.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 30 '22 at 20:56