12

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

3 Answers 3

18
"C:\Program Files\Git\git-bash.exe" --cd=c:\path\to\folder
1
  • 3
    This wasn't work for me. I was mystified by this, until I checked ~/.bashrc. Lo-and-behold, the first line of the script was cd $GIT_HOME. I commented out this line and now the --cd argument is working. Commented Jul 7, 2021 at 12:54
6

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.

6
  • 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.
    – Luvexina
    Commented May 2, 2020 at 2:12
  • Can you please clarify? I don't understand what you mean by "undo". Commented May 2, 2020 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.
    – Luvexina
    Commented May 2, 2020 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. Commented May 2, 2020 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.
    – Luvexina
    Commented May 2, 2020 at 2:46
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .