Git: Open Git-Bash in specific directory

"C:\Program Files\Git\git-bash.exe" --cd=c:\path\to\folder

One possible solution is to change the directory before opening git-bash. By default, git-bash opens in whatever the current directory is. To do this, put a cd call before the start,

cd C:\specific\dir\to\open && start "" "C:\Program Files\Git\bin\sh.exe"

Since this is being done in SAS, the specific directory can be stored in a macro variable. This guarantees the requirement of being within 50 characters (and therefore callable from a hotkey in the KEYS menu). Somewhere in your code you can assign the Git Working Directory,

%let gwd = C:\specific\dir\to\open;

The %sysexec call then looks like

%sysexec(cd &gwd. && start "" "C:\Program Files\Git\bin\sh.exe" && exit);

This works as follows. First, SAS will expand &gwd. It then opens a Windows Command Prompt. The cd changes directories to whatever &gwd. resolved to. Git-bash then opens in the current directory (which was changed to &gwd.). Finally, whenever git-bash closes, the exit command is given, closing the Windows Command Prompt session.

Unfortunately, it seems like the initial cd introduces just enough lag between the call and the opening of the git-bash to be annoying. I suspect that issuing a cd command within git-bash might be faster, but this approach does work.


You can create a script such as this:

cd c:\path\to\particular\directory
"C:\Program Files\Git\bin\sh.exe" 

Save that as either whatever.cmd or whatever.bat and double click it.

For convenience, you can add your script to the taskbar with the steps below. Thought I'd include this since it's not quite as easy as you might expect (can't simply drag the script itself to the taskbar):

  1. right click the script and select "Create shortcut"
  2. right click the shortcut you created and select "Properties"
  3. Under Shortcut, Target, enter: cmd /c C:\path\to\your\script\whatever.cmd
  4. Note: you may need to specify the full path to cmd (e.g., C:\Windows\System32\cmd.exe...)

Now simply click the command on your taskbar to run your script with one click.