Adding “Open in Git Bash” to the Context Menu

One thing I use a lot on my computer is the “open command window here” context menu entry.  By default, you have to hold shift when right-clicking on a folder to get it to show up, but you can hack it such that it always shows up when you right-click on a folder (delete the registry entry at HKCR\Directory\shell\cmd called Extended) or when you right-click the empty space inside a folder window (delete the registry key at HKCR\Directory\Background\shell\cmd called Extended).

The Windows version of git comes with a version of bash that’s set up all nice-like with git integration.  I found myself often opening a bash console, switching into some directory, doing something with git, and then closing the console.  That’s somewhat tedious, though, so I ended up making an “Open in Git Bash” context menu entry.  If you want to use it, you’ll need two files: bash.bat and bash.reg

bash.bat (for 64-bit systems)

@%~d1
@cd "%~1" > NUL

@C:\Windows\SysWOW64\cmd.exe /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"

bash.bat (for 32-bit systems)

@%~d1
@cd "%~1" > NUL

@C:\Windows\cmd.exe /c ""C:\Program Files\Git\bin\sh.exe" --login -i"

bash.reg

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Directory]

[HKEY_CURRENT_USER\Software\Classes\Directory\Background]

[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell]

[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\bash]
@="Open in Git &Bash"

[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\bash\command]
@="C:\\Path\\To\\File\\bash.bat \"%V\""

[HKEY_CURRENT_USER\Software\Classes\Directory\shell]

[HKEY_CURRENT_USER\Software\Classes\Directory\shell\bash]
@="Open in Git &Bash"

[HKEY_CURRENT_USER\Software\Classes\Directory\shell\bash\command]
@="C:\\Path\\To\\File\\bash.bat \"%V\""

Put bash.bat and bash.reg somewhere.  Edit the lines I highlighted in bash.reg to point to wherever you put bash.bat.  Note: it is vital that you use double-slashes (\\) for the directory separators.  Once you’ve done that, go ahead and double-click bash.reg to merge it into your registry.  Now try right-clicking on a folder and marvel at the new option you have.  If you want, you can delete bash.reg now.

Tags: , ,

Sunday, July 17th, 2011 Thoughts

16 Comments to Adding “Open in Git Bash” to the Context Menu

  • […] found what I was looking for over here, and it didn’t take 5 minutes to […]

  • Wilfred Knievel says:

    Super helpful script! One little addition: it might be worth starting the batch file with the line

    @%~d1 > NUL

    so the git repository can be on a different drive.

  • Ricardo says:

    Thanks for sharing such a great resource tip.

  • Chris says:

    This worked perfectly – thank you, just what I was looking for. The default msysgit options annoyed me – I wanted just Git Bash here and nothing else. Removing msysgit's defaults then adding a modified version of what's here did the trick. Surprised it took 2 reg keys for it to work properly, but there aren't any repeats in the resulting context menus.

    • elyscape says:

      The reason it takes two registry keys is that one is used when you right-click on a folder and the other is used when you right-click the background of an Explorer window.

  • Dave Stewart says:

    Great post!

    I updated the .reg slightly to only have the prompt show when SHIFT + right-clicking, as I find that less obtrusive. Note its’ just the “Extended”=”” entries which are new.

    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USERSoftwareClassesDirectory]

    [HKEY_CURRENT_USERSoftwareClassesDirectoryBackground]

    [HKEY_CURRENT_USERSoftwareClassesDirectoryBackgroundshell]

    [HKEY_CURRENT_USERSoftwareClassesDirectoryBackgroundshellbash]
    @=”Open in Git &Bash”
    “Extended”=””

    [HKEY_CURRENT_USERSoftwareClassesDirectoryBackgroundshellbashcommand]
    @=”C:\Program Files (x86)\Development\Git\bin\bash.bat “%V””

    [HKEY_CURRENT_USERSoftwareClassesDirectoryshell]

    [HKEY_CURRENT_USERSoftwareClassesDirectoryshellbash]
    @=”Open in Git &Bash”
    “Extended”=””

    [HKEY_CURRENT_USERSoftwareClassesDirectoryshellbashcommand]
    @=”C:\Program Files (x86)\Development\Git\bin\bash.bat “%V””

    Thanks again,
    Dave

  • Phil says:

    What does "%~d1" and "%~1" mean? In 20 years of batch file authoring I have never seen these before; my Windows XP says "The system cannot find the path specified." for the statement "cd "%~1" > NUL"; and Google can't search for such strings (it just shows me something about a Sony d1 device).

    • elyscape says:

      Are you passing in some path as an argument? %1, as you probably know, is the first argument. %~1 suppresses quotes around a path and %~d1 suppresses quotes and leaves only the drive. So if you make a batch file test.bat with this in it:

      @echo off
      echo %1
      echo %~1
      echo %~d1

      And call it like this:

      test.bat "C:Program FilesWindows NThypertrm.exe"

      Your output should be:

      "C:Program FilesWindows NThypertrm.exe"
      C:Program FilesWindows NThypertrm.exe
      C:

      As for where I found out about this, the documentation for the FOR command has information about it. Scroll down to the Variable Substitution section. It’s probably worth noting that I’m pretty sure this only works in batch files and in the FOR command.

      • Phil says:

        Sweet! Thanks a lot – I must admit I wasn’t aware of that usage.

        So would these two lines have the same effect?
        @cd “%~1” > NUL
        @cd %1 > NUL

        Also I tend to start my batch files with @ECHO OFF so that I can omit the @ prefix for every line after that. Not sure if that would make your example any clearer.

        It would be really nice if we could specify the appropriate initial working directory (from the user’s %1) for the new prompt directly in the ‘data’ string of the shellbashcommand@ value so that the registry change would be sufficient without needing a batch file.

  • Manfred Meissner says:

    It is a lot easier if you put a Git Bash.vbs shortcut in your "send to" folder and use that to launch the git bash out of any folder by right click- send to

  • Dude says:

    How do you add git-bash.exe in context menu?

  • John says:

    You can install git for windows or Github for windows , both give you the choice while installing to add this feature to your windows explorer.

  • Chandan says:

    Awesome !

  • Leave a Reply to Phil