128

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.

2
  • 2
    @StevenVascellaro Edited question to explain why this isn't a dupe. Commented May 7, 2018 at 15:50
  • That makes sense. I have retracted my duplicate vote. Commented May 7, 2018 at 15:54

10 Answers 10

92

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"

Sign up to request clarification or add additional context in comments.

3 Comments

How would I use the %1 registry parameter?
current answer is soo obsolete, this one should be the one!
This seems ment for a batch file. (.bat)
75

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.

4 Comments

Kudos! Never knew Start in had something to do with the working directory a program thinks it's in.
This is the easiest method. Afterwards, I pinned that short cut to task bar then Windows + 1, I keep that as first item of the task bar, opens the repo with git-bash directly.
If you have 64-bit git installed, the command is "%SYSTEMDRIVE%\Program Files\Git\bin\sh.exe" --login
not working for me in Win 11 anymore. Worked fine in Win 10... EDIT: still works fine. Default was some -cd-to-Home param added to the shortcut. Remove this and the solution works again
53

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

3 Comments

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
You are right! I was missing the fact that you cannot simply cd x:\path. Will edit my solution accordingly.
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!
37

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\" 

4 Comments

When paired with C:\Program Files\Git\usr\bin\bash.exe --cd=<path> 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."
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, did you really use "<path>"? ...instead of "%path%"? No wonder, it did not work.
IIRC %path% is a Windows ENV variable, so that wouldn't work either. And to directly address your passive aggressive comment: Obviously no, <value>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.
33

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

Comments

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

2 Comments

For the record, the default git bash entry inside the registry at HKEY_CLASSES_ROOT\Directory\Background\shell\git_shell\command is "C:\Program Files\Git\git-bash.exe" "--cd=%v."
Nice! Thanks, @Ferrybig! ^_^
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 

3 Comments

it runs new cmd in windows 10
@kyb it's supposed to be /b for Win 10
@kyb I made an answer that is a modification of lengxuehx's answer, in case you or anyone else is still interested: stackoverflow.com/a/39779451/3543437
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.

1 Comment

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.
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.

1 Comment

@MichaelMcQuade It's a hint and okay, but doesn't answer the question, how to open a git-bash by a script
3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.