110

How can I launch a new Git Bash window with a specified working directory using a script (either Bash or Windows batch)?

My goal is to launch multiple Git Bash windows from a single script, each set to a different working directory. This way I can quickly get to work after booting the computer instead of having to open Git Bash windows and navigating each one to the correct working directory.

I am not asking how to change the default working directory, like this question does, but to launch one or more terminal windows with different working directories from a script.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
holocronweaver
  • 2,171
  • 3
  • 18
  • 20

10 Answers10

76

Try the --cd= option. Assuming your GIT Bash resides in C:\Program Files\Git it would be:

"C:\Program Files\Git\git-bash.exe" --cd="e:\SomeFolder"

If used inside registry key, folder parameter can be provided with %1:

"C:\Program Files\Git\git-bash.exe" --cd="%1"

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Mike Kowalski
  • 781
  • 5
  • 2
71

Another option is to create a shortcut with the following properties:

enter image description here

Target should be:

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

Start in is the folder you wish your Git Bash prompt to launch into.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
52

Git Bash uses cmd.exe for its terminal plus extentions from MSYS/MinGW which are provided by sh.exe, a sort of cmd.exe wrapper. In Windows you launch a new terminal using the start command.

Thus a shell script which launches a new Git Bash terminal with a specific working directory is:

(cd C:/path/to/dir1 && start sh --login) &
(cd D:/path/to/dir2 && start sh --login) &

An equivalent Windows batch script is:

C:
cd \path\to\dir1
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login 
D:
cd \path\to\dir2
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login 

To get the same font and window size as the Git Bash launched from the start menu, it is easiest to copy the start menu shortcut settings to the command console defaults (to change defaults, open cmd.exe, left-click the upper left icon, and select Defaults).

holocronweaver
  • 2,171
  • 3
  • 18
  • 20
  • 1
    This works for me: `cd` to the directory I want Git Bash to start, and then running the `start` command in your example will really give me a Git Bash terminal in that dir. Are you sure you are not doing a `cd $HOME` (or `cd` without args) somewhere in your bash rc files? (Like `.bashrc`, `.profile`, ...) Also keep in mind the drive letter. If you want to start Git Bash on the `X:` drive, then doing `cd x:\path` won't cut it, you have to do `x:` and then `cd \path` before `start` – janos Nov 14 '13 at 22:24
  • 1
    You are right! I was missing the fact that you cannot simply `cd x:\path`. Will edit my solution accordingly. – holocronweaver Nov 15 '13 at 00:44
  • 1
    A perfect solution! Let me only add, that default `cmd.exe` settings are **not** the same, as default Git Bash settings upon Git Bash installation! And you have to understand differences between default settings (`Defaults` from context menu) and current one (`Properties`). So, what I did was: (1) open Git Bash, select `Properties` (current settings), (2) tweak it, to exactly, what I want (font, window size + position), (3) open `cmd.exe`, select `Defaults`, (4) copy-paste settings from Git Bash to `cmd.exe`. Then `OK` in `cmd.exe`'settings, `Cancel` in `Git Bash`'s settings and voila! – trejder Jan 17 '14 at 13:45
34

Let yet add up to the answer from @Drew Noakes:

Target:

"C:\Program Files\Git\git-bash.exe" --cd=C:\GitRepo

The cd param should be one of the options how to specify the working directory.

Also notice, that I have not any --login param there: Instead, I use another extra app, dedicated just for SSH keys: Pageant (PuTTY authentication agent).

Start in:

C:\GitRepo

The same possible way, as @Drew Noakes mentioned/shown here sooner, I use it too.

Shortcut key:

Ctrl + Alt + B

Such shortcuts are another less known feature in Windows. But there is a restriction: To let the shortcut take effect, it must be placed somewhere on the User's subdirectory: The Desktop is fine.

If you do not want it visible, yet still activatable, place this .lnk file i.e. to the quick launch folder, as that dir is purposed for such shortcuts. (no matter whether displayed on the desktop) #76080 #3619355

"\Application Data\Microsoft\Internet Explorer\Quick Launch\"
Franta
  • 986
  • 10
  • 17
  • When paired with `C:\Program Files\Git\usr\bin\bash.exe --cd=` it will just flash and stop as if the command was not supported. For example `bash.exe --login` works. `bash.exe --xyz` flashes and exits in the same way. Is there a way to run this with a configured path? Use case: registry entry for right click "Start bash here" `"C:\Program Files\Git\usr\bin\bash.exe" "--cd=%v."` – Qwerty Apr 28 '23 at 04:50
  • OMG ‍♂️. I literally spent two days figuring that out and just doing `"C:\Program Files\Git\usr\bin\bash.exe"` without any params launches the program in the correct directory. ‍♂️ – Qwerty Apr 28 '23 at 09:09
  • @Qwerty, did you really use ""? ...instead of "%path%"? No wonder, it did not work. – Franta Jun 11 '23 at 22:14
  • IIRC `%path%` is a Windows ENV variable, so that wouldn't work either. And to directly address your passive aggressive comment: Obviously no, ``is a common way of representing a placeholder for user's value, moreover you can clearly see the actual value I used in the very same comment. Anyway you are completely missing the point here, which is that providing any `--cd` value caused the program to crash as if it was an invalid command altogether. – Qwerty Jun 13 '23 at 00:01
29

In addition, Win10 gives you an option to open git bash from your working directory by right-clicking on your folder and selecting GitBash here.

enter image description here

monkrus
  • 1,470
  • 24
  • 23
7

Windows 10

This is basically @lengxuehx's answer, but updated for Win 10, and it assumes your bash installation is from Git Bash for Windows from git's official downloads.

cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit

I ended up using this after I lost my context-menu items for Git Bash as my command to run from the registry settings. In case you're curious about that, I did this:

  1. Create a new key called Bash in the shell key at HKEY_CLASSES_ROOT\Directory\Background\shell
  2. Add a string value to Icon (not a new key!) that is the full path to your git-bash.exe, including the git-bash.exe part. You might need to wrap this in quotes.
  3. Edit the default value of Bash to the text you want to use in the context menu enter image description here
  4. Add a sub-key to Bash called command
  5. Modify command's default value to cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit enter image description here

Then you should be able to close the registry and start using Git Bash from anywhere that's a real directory. For example, This PC is not a real directory.

enter image description here

kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
4

This is the command which can be executed directly in Run dialog box (shortcut is win+R) and also works well saved as a .bat script:

cmd /c (start /d "/path/to/dir" bash --login) && exit
asdfg_rocks
  • 117
  • 1
  • 11
lengxuehx
  • 1,500
  • 1
  • 18
  • 25
3

I'm not familiar with Git Bash but assuming that it is a git shell (such as git-sh) residing in /path/to/my/gitshell and your favorite terminal program is called `myterm' you can script the following:

(cd dir1; myterm -e /path/to/my/gitshell) &
(cd dir2; myterm -e /path/to/my/gitshell) &
...

Note that the parameter -e for execution may be named differently with your favorite terminal program.

Marcus Rickert
  • 4,138
  • 3
  • 24
  • 29
  • Thanks, your answer got me thinking in the right direction! That is how I do things in Linux, but I could not figure out the equivalent in MS Windows...until now! For Git Bash, `myterm` is cmd.exe with additions via MSYS/MinGW. To launch a new cmd, you use the Windows-specific `start` command followed by the path to Git Bash's `sh.exe` and then any command line options you like. My answer gives details. – holocronweaver Nov 12 '13 at 04:06
3

Using Windows Explorer, navigate to any directory you want, type "cmd" in the address bar it will open Windows command prompt in that directory.

Along the same lines, if you have the git directory in your path, you can type "git-bash" in the address bar and a Git Shell will open in that directory.

giraffesyo
  • 4,860
  • 1
  • 29
  • 39
  • @MichaelMcQuade It's a hint and okay, but doesn't answer the question, how to open a git-bash by a script – jeb Mar 12 '21 at 07:08
2

If using Windows OS :

  1. Right click on git terminal > Properties

Step > one

  1. Properties>Under shortcut tab>Start in: add your folder target path like below image

Step > two

Surendranath Sonawane
  • 1,595
  • 1
  • 18
  • 24