74

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,452
  • 1
  • 13
  • 18

8 Answers8

141

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,265
  • 21
  • 36
Tarun
  • 2,808
  • 3
  • 22
  • 21
  • 25
    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
  • 17
    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
  • 10
    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
  • 6
    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
  • In VS2022 there is no Initial Dir any more. I tried --cd="$(SolutionDir)\" but that fails :( – noelicus Aug 04 '22 at 10:17
48

Visual Studio 2019 after v16.6 comes with an integrated terminal similar to the one in Visual Studio Code. It also doesn't come with a preconfigured git bash, so in order to make it work you'll have to create the configuration.

  1. Open a terminal ("View -> Terminal") and click on the gear icon in the terminal window. Alternatively, go to "Tools -> Options" then "Environment -> Terminal"
  2. Click the Add button.
  3. 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 to get your default aliases and so on. The shell will be started in your solution's base directory. Note that the name in the list will not update before you close the dialog (as of VS 2022 17.3.1).

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

As of VS 2022 17.3.1 there are two major bugs that I'm aware of:

  1. if you try to open your new bash, and you get a Powershell instead, the only way I'm aware of to fix that and to actually be able to open the bash is to delete the Powershell configuration that was used instead. If you need it, you should save its settings and restore them manually.
  2. if the Shell Location is set to a full path including the bash.exe executable, clicking the button labelled "..." on the right, which opens a file selection dialog, will make Visual Studio crash hard.
tobi_s
  • 1,324
  • 13
  • 16
  • 2
    is the `--login -i` argument required? it works fine without it – Shevek Jul 10 '20 at 09:18
  • I'm not sure, it may depend on your PATH and your .bash_login, .profile etc – tobi_s Jul 11 '20 at 02:59
  • @Shevek without it e.g. alias "ll" won't be defined – Roman Orekhov Nov 07 '21 at 22:43
  • Since a large part of this was about a bug that was fixed some two years ago, and since it's still useful, I updated this to reflect the current reality. – tobi_s Aug 18 '22 at 07:49
  • 2
    Point #1 you mentioned at the bottom was my solution. Deleting the powershell config and only having the git bash config did the trick - thanks! – Shane Creedon Sep 09 '22 at 21:33
  • Worked when I kept the powershell as secondary option and gitbash as the primary terminal in VS 2022. – Huzaim Oct 31 '22 at 05:54
  • Thanks for the VS 22 edit! – qslabs Nov 17 '22 at 21:02
  • Regard with your comment: "As of VS 2022 17.3.1 there are two major bugs that I'm aware of:" You dont need to delete the Poweshell one you can just make Bash the as default terminal and it will work like charm – Kiarash Nov 30 '22 at 00:48
  • @Kiarash if it works now that's great! I'm leaving this information in place for people who might still encounter the bug (like Shane Creedon did 6 weeks ago). I think it's clear enough that this may or may not happen. – tobi_s Nov 30 '22 at 03:37
  • 1
    I followed these steps exactly, but my Git Bash terminal closes immediately upon opening it. To launch the terminal, first I open a Terminal view (View > Terminal) To the right of the gear icon, I open the drop down menu and select the "Git Bash" terminal that I created from the instructions above. When I do this, the terminal opens then closes immediately. – Ramza Jan 22 '23 at 19:53
  • @Ramza I'm not a support line, but it sounds like you don't have bash installed or in a different path. I would suggest to try what happens if you try to run c:\program files\git\bin\bash.exe in a command line window, and go from there. – tobi_s Jan 23 '23 at 03:24
20

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
  • 389
  • 3
  • 8
  • 3
    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
13

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
ihor.eth
  • 2,207
  • 20
  • 27
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
  • 290,304
  • 63
  • 469
  • 417
  • 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
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
  • 6,028
  • 3
  • 40
  • 40
  • 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
  • 75,175
  • 8
  • 100
  • 122
1

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
  • 330
  • 2
  • 6
  • `/c ""%VSAPPIDDIR%\..\Tools\VsDevCmd.bat" -arch=x64 & "\bin\bash.exe" --login -i"` might be more working version because path to git usually has spaces – jikuja Nov 09 '22 at 10:11