59

I have a windows version of Git Bash, and I want to use it with my Visual Studio Projects. What I want is a way of tweaking some setting so that one click can get Bash opened with the directory on the current visual studio project.

It would be great if there's a way to set up short cut in Git Bash for directories, so that one command can get me to a preset directory.

Andy
  • 1,256
  • 1
  • 10
  • 18

8 Answers8

120

Launch Git Bash from Visual Studio

In Visual Studio Menu Bar: Tools -> External Tools -> Add New

Configuration:

Name: Git Bash

Command: c:\Program Files (x86)\git\bin\sh.exe

Args: --login -i

Initial Dir : $(SolutionDir)

weeksdev
  • 4,217
  • 20
  • 35
Tarun
  • 2,578
  • 3
  • 21
  • 21
  • 23
    You can type the name like Git &Bash so that you can use the keyboard shortcut ALT+T+B for it – Daniel B Feb 23 '16 at 05:23
  • If it doesn't work restart Visual studio. Then it will work – mjwrazor Jun 20 '16 at 01:32
  • 1
    This is so cool, and no extra stuff to install. For me, I do a lot with Open Website rather than solutions, so the solution files are in a weird temp directory. To get the right initial directory, I've made the Initial Directory be the location of whatever file is opened. Also I seem to have Git installed in a different directory (maybe I selected install just for me). **Title**: `Git &Bash` **Command**: `%LocalAppData%\Programs\Git\bin\sh.exe` **Arguments**: `--login -I` **Initial directory**: `$(ItemDir)` – zacharydl Mar 18 '17 at 07:34
  • 15
    Instead of `sh.exe`, use `git-bash.exe` to support copy from- and paste to- bash. There is no need to add `--login -i` arguments, it's just like click `Git Bash Here` on the solution folder. – Eido95 Mar 31 '17 at 17:00
  • 8
    In addition you can add your own shortcut in **Tools -> Options -> Evironment -> Keyboard** -> Search for "Tools.ExternalCommand[X]", where X is the position of the Git Bash command in the external tools menu. Then choose your favorite shortcut – Steven Apr 21 '17 at 12:53
  • 4
    the above command line doesn't start the real "git bash here" looking nice and colorful. To start it use: command C:\Program Files\Git\git-bash.exe arguments --cd="$(SolutionDir)\" And don't remove the ending slash from my example, because the SolutionDir always ends with slash, which is escaping the ending quote ... – LyuboBG Jun 12 '18 at 11:15
  • Instead of navigating using "alt+T..." for example, you could also use quick search "CTRL+Q" to search for the tool. Its not less to type, but if you make heavy use of CTRL+Q anyway, its convenient. – martinoss May 29 '20 at 08:22
  • Is there a way this to be emdeed window in vs instead of standalone window? – Henry Aug 05 '20 at 06:51
  • @Henry see [tobi_s's answer below](https://stackoverflow.com/a/62126440/3271599) – Aaroninus Nov 02 '20 at 20:31
28

Visual Studio 2019 after v16.6 comes with an integrated terminal similar to the one in Visual Studio Code but its configuration dialog is really buggy as of now. It also doesn't come with a preconfigured git bash, so in order to make it work, you'll have to work around the bugs and create the configuration.

  1. Go to "Tools -> Options" then "Environment -> Terminal"
  2. Before changing anything, click the Add button.
  3. If the bug is present, you will see that your newly created profile has "(Default)" printed behind its name. You now have two default profiles and the integrated terminal as well as the configuration dialog will do weird stuff. If that is the case select the the second profile, i.e. the one that doesn't say "(Default)", click "Set as Default." Now there should only be one default profile left, and everything should behave as normal. In particular, you can set the default to whatever you want. Just make sure that you don't have the default profile selected when you create a new profile because then you'll end up with two default profiles again. It is important to always keep a second profile around, because otherwise you can only add default profiles, which breaks the GUI.
  4. Configure the bash shell: set the name to something you like (I use "git bash" because I"m boring), as executable use c:\program files\git\bin\bash.exe, as Arguments use --login -i. The shell will be started in your solution's base directory.

You should now be able to use bash in the integrated terminal via the default shortcut Ctrl + ` or via View -> Terminal in the menu.

tobi_s
  • 1,084
  • 11
  • 14
12

In VS2017 I added mine by going to Tools -> External Tools -> Add

  • Title: Git Bash
  • Command: C:\Program Files\Git\git-bash.exe
  • Initial directory: $(SolutionDir)

Note: Use Output window runs the command and puts any output into the Output window. This is useful for commands that just return some data so I didn't check it.

If you want to create a shortcut for it:

  1. Use MoveUp to move 'Git Bash' to the 1st position.
  2. Go to Tools -> Options -> Environment -> Keyboard and search for Tools.ExternalCommand1
  3. Assign shortcut
ihorbond
  • 1,845
  • 15
  • 23
11

Have a look at Git Source Control Provider: http://gitscc.codeplex.com/

It has Visual Studio integration for Git and one of the options is to bring up Git Bash.

manojlds
  • 275,671
  • 58
  • 453
  • 409
  • For Visual Studio 2013 users there is a tighter Git integration where you do not need Git Source Control Provider; Visual Studio 2013 provides a Git source control provider out-of the-box. @Taran's answer is the better solution for Visual Studio 2013. – RunnerRick Apr 25 '14 at 17:45
9

you can to do this to add git bash (no install anything):

  1. Open tools

  2. Open terminal and push Add button

  3. Set this config (or your machine config) enter image description here

  4. Apply: set as default and Ok

  5. open one terminal View -> Terminal

enter image description here

  1. Add a new terminal (1) and select "git bash" (2) (this open a new tab 3)

enter image description here

can to close powershell terminal tab.

git bash tab open automatically next time that you open VS? Yes.

if you close Git bash tab? Open a new powershell terminal again and add Git bash terminal (step 5, 6)

Marbin274
  • 179
  • 2
  • 6
  • 2
    Important distinction shown in this answer that got it working for me. The Shell location should be "C:\Program Files\Git\bin\sh.exe" and not "C:\Program Files\Git\git-bash.exe". Thanks friend! – IdusOrtus Apr 02 '21 at 17:49
7

I'll recommend you using GIT Tools for Visual Studio 2013 onwards

You can know its complete help information by visiting http://yysun.github.io/git-tools/#/

To install it go in Tools -> Extensions and Updates and type Git Tools. Download the plugin and install or you can simply visit visual studio market place https://marketplace.visualstudio.com/items?itemName=yysun.GitTools and click on download.

Snapshot of Git Tools

Once you have installed Git Tools extension and restarted all opened instances of visual studio

To Launch Git Bash: * Click on Git Tools -> Git Bash as shown below

enter image description here

enter image description here

However before performing these steps please ensure that you have git bash https://git-scm.com/downloads installed completed on your environment and most probably configured your logins if possible.

vibs2006
  • 5,429
  • 3
  • 37
  • 37
  • Why this should be better than the Accepted one (calling the sh.exe)? Please explain on the answer – EduLopez Apr 14 '19 at 16:02
  • @EduLopez there are other advantages of using gittools like its changelog UI screen (diff viewer) doesn't require an installation of any third party tool like Kdiff3 or p4merge etc. It has inbuilt functionality for that. The default diffviewer with git bash setup is a very basic one and it works only in dos thus we cannot use mouse movements to analyse the differences. It is based on VI Editor of Linux Bash. – vibs2006 Apr 15 '19 at 03:05
3

You can use posh-git in the Nuget Package Manager Console, which automatically switches to the location of the open solution.

dahlbyk
  • 71,602
  • 8
  • 96
  • 121
0

Visual Studio 2019 integrated terminal solution calling bash directly is Ok, but doesn't provide developer environment. If you need it, use cmd.exe and start bash.exe from within. It can be automated via the same Tools/Options.../Environment/Terminal dialog:

  1. Shell location: C:\WINDOWS\system32\cmd.exe
  2. Arguments: /c ""%VSAPPIDDIR%\..\Tools\VsDevCmd.bat" -arch=x64 & <path\to\git-for-windows>\bin\bash.exe --login -i"

Notes:

  1. -arch=x64 selects development environment architecture
  2. --login -i e.g. sets correct "LANG" env var so that non-English filenames show correctly, also enables e.g. alias 'll' and filename colors
  3. /c instead of /k so that after you exit from bash, the command prompt exits as well
Roman Orekhov
  • 141
  • 1
  • 4