Fork me on GitHub

Git Bash Inside PhpStorm

利用Git Bash替换PHPstorm中默认的terminal

If you don't use Git and Jetbrains' fabulous editor, PhpStorm, this article probably won't interest you. Likewise, if you use both and are used to using the Git features inside PhpStorm, this won't be much use to you, although you can do other things in Git Bash like using SSH connections, copying files, running Grep, or issuing Composer or Perl commands. Of course you can also use it as a terminal.

I'm used to using the Git Bash command line tool. I trust it more than I do any IDE-based Git tool. The command line tool also allows you to quickly and safely perform the more complex operations necessary to recover from the disasters that will eventually strike when you're working in the wee hours of the morning.

Of course I already had a Quick Start icon for the Bash shell at the bottom of my screen, so I could launch it any time, but it's much more convenient to issue the commands in PhpStorm where the files you've been working on are visible. On Windows, another huge plus is that you have full copy and paste capability both ways (something I didn't get in the Bash window launched from the Windows desktop). You can copy your commit message and paste it into your changelog, for example (or vice versa).

It took me most of an evening and a lot of trial and error to make this work and Google wasn't much help, so I thought I'd document it here for others. These steps are for PhpStorm 2016.2 with XAMPP on localhost under Windows 10, so you'll have to modify them somewhat if you're on another platform. This assumes that you have a Git client and the Git Bash shell. I use the shell installed with Git (installed from here). A similar process should work with cygwin or other shells.

The Terminal

I kept playing with various PhpStorm settings for Command Line Tools, since that's what I think of Git Bash as (it looks and acts like the Windows cmd prompt). The fact is, though, that it's a terminal client and that's how PhpStorm thinks of it. To implement it, you have to go to:

File -> Settings -> Tools -> Terminal

Once you're there, here are the steps necessary to enable Git Bash in the Terminal dialog.

Put the following line in the Shell Path field (adjust as necessary for your platform):

"C:Program Files (x86)Gitinsh.exe" -login -i

If you are using the 64-bit version of Git, the path is different:

"C:Program FilesGitinsh.exe" -login -i

Don't forget the quotation marks. They're necessary for PhpStorm to distinguish between the file and the options. Note that login has two dashes, but i has only one. You can add -v if you like verbose output from Bash. The -login tells Bash to run it's configuration files (more on that in the next article), and the -i tells Bash that you want interactive mode.

As an alternative, you can click on the browse icon and navigate to the sh.exe file, wherever it is, but be sure to add the command-line options after the path.

I left all the checkboxes checked and it seems to work fine. When you're done, click on "Apply" and then on "OK". You may have to restart PhpStorm for this all to take effect.

Use the "Test" button to make sure you have the path correct.

If you haven't already, you also need to set the path to Git. Go to:

File -> Settings -> Version Control -> Git

Set the path to:

C:Program Files (x86)Gitingit.exe

or this if you're using the 64-bit version of Git:

C:Program FilesGitingit.exe

As with setting up the shell, you can also use the browse button. Quotation marks are not necessary here, since there are no command-line options.

Launching the Terminal

At the bottom of the PhpStorm screen (bottom left on my version) you should see a "Terminal" button. When you click on it, you should see a window with the Bash prompt, followed by a cursor. If the cursor is white, your typing will be ignored until you click in the window — then it will turn black. If you don't see that "Terminal" button, you can use the View -> Toolbars menu option or Alt-F12 to bring you the terminal window.

I prefer to have the window floating. You can select that option from the little gear icon at the upper right of the window. Clicking on the minimize icon next to the gear icon, or on the "Terminal" button will hide the window. If you click on the red X, it will close your terminal session and you'll lose your place. You can also have multiple Bash windows open by clicking on the + symbol, which is handy if you want to repeat actions in different directories, especially since you can use copy and paste.

标签: IDE, PHPstorm, Git-Bash