7

I am able to start git-bash via the Windows command prompt in several ways:

"C:\Program Files\Git\bin\sh.exe"
"C:\Program Files\Git\git-bash.exe"
"C:\Program Files\Git\usr\bin\mintty.exe"

I would like to start git-bash in a particular directory using such a call.


For context, I am doing this from within the SAS Enhanced Editor. The actual call is

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

For those unfamiliar with SAS, %sysexec opens an instance of the Windows Command Prompt and issues whatever command is given as an argument. It works surprisingly well. However, I would like to be able to start the git-bash in a particular directory, not just home.

To this end, I can create an entry in .bashrc:

cd /c/new/starting/dir

The trouble with doing this, though, is whenever I open a git-bash, such as via the context menu in a particular folder, the default directory is that of .bashrc.

I see that there exist git-bash start options like --cd-to-home or --cd=<path> but I can't get them to work. For example,

"C:\Program Files\Git\bin\sh.exe" --cd-to-home
"C:\Program Files\Git\bin\sh.exe --cd-to-home"
"C:\Program Files\Git\git-bash.exe" --cd-to-home
"C:\Program Files\Git\git-bash.exe --cd-to-home"
etc.

Is it possible to start git-bash in a particular directory from Windows Command Prompt? If so, what's the proper syntax?

BONUS: Bonus points for doing it in 50 characters or less

5

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.

| improve this answer | |
  • Is there any way to undo this? I realized that I wanted to start this wherever I start it up, not in the same folder every time. – VFDan May 2 at 2:12
  • Can you please clarify? I don't understand what you mean by "undo". – Lorem Ipsum May 2 at 2:22
  • I would like to startup in the folder that I called it in, not the folder that I set GIt Bash to cd into on startup. – VFDan May 2 at 2:26
  • You will need a way to get the folder you want. Assign that to a variable and then update the cd statement to use that variable. I suspect you'll run into issues of quoting or finding how to dynamically change the string. Since I don't know what language you're using, I can't be more precise than that. However, because how it's done depends on the language, it may be better to open a separate question. – Lorem Ipsum May 2 at 2:40
  • What do you mean? I meant that I would like my Git Bash to go to the same place that I start it in; i.e. when I right-click and press "Git Bash here" I want it to start in that folder, not the folder I set it to in this answer. – VFDan May 2 at 2:46
3
"C:\Program Files\Git\git-bash.exe" --cd=c:\path\to\folder
| improve this answer | |
2

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.