42

I'm trying to set Git executable in IntelliJ to be the git installed in Windows Subsystem for Linux, I tried a few different ways, but always got some sort of error. Today I installed to Creators Update (Version 1703), reinstalled WSL and tried again, here's what I did:

I created a .bat script:

@echo off
C:\Windows\System32\bash.exe -c "git %*"

So when running it:

C:\Users\Limon\Desktop>bash.bat --version
git version 2.7.4

So then I tried to set this bat at the git executable in IntelliJ: Setting Git executable in IntelliJ

And it worked! But everything else fails, for example when I try to pull or branch in IntelliJ, I get:

Couldn't check the working tree for unmerged files because of an error.
'C:\Windows\System32\bash.exe' is not recognized as an internal or external command,
operable program or batch file.

Any ideas on how fix this? I don't really know anything about batch scripting. It works perfectly off command line.

Limon
  • 963
  • 2
  • 10
  • 23

11 Answers11

22

I was looking for a way to use git on WSL Windows Subsystem for Linux through Webstorm or an IntelliJ idea software.

I tried KatoPue's solution, but I got the following error:

fatal: could not read log file 'C:/Program Files/Git/mnt/c/Users/Elies/AppData/Local/Temp/git-commit-msg-.txt': No such file or directory

I solved it by replacing the path when sending the command to WSL's git

Settings > Version Control > Git > Path to Git executable : path_to_wslgit.bat

wslgit.bat :

@echo off
setlocal enabledelayedexpansion
set command=%*
set find=C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt
set replace=/mnt/c/Users/%USERNAME%/AppData/Local/Temp/git-commit-msg-.txt
call set command=%%command:!find!=!replace!%%
echo | C:\Windows\Sysnative\bash.exe -c 'git %command%'
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
Elies Lou
  • 351
  • 2
  • 5
  • When I try this, the command never returns from intellij; it just hangs forever. Running it on command line works fine, though (as `git.bat config --list`, for instance). Also, the `%string%` var wasn't defined, leading to "echo is off" in the log file. I tried adding `--no-pager` to the git command on the last line, but it didn't help. – yshavit Dec 22 '17 at 23:46
  • Ah, got it. You can fix this by piping an empty string to bash.exe: `echo | bash.exe ...` – yshavit Dec 23 '17 at 03:21
  • 1
    This answer worked for me on VS Code just fine! Kudos and thank yous all around! – JPMC Feb 15 '18 at 07:58
  • If you get an error "\bash.exe' is not recognized as an internal or external command, operable program or batch file." then try `C:\Windows\Sysnative\bash.exe` . – chx Mar 29 '18 at 21:21
  • When I use this I get the error `could not read log file 'C:UsersNameAppDataLocalTempgit-commit-msg-.txt': No such file or directory`, apparently all the slashes are getting removed somehow. – Hobo Joe Aug 13 '19 at 17:33
  • As mentioned by @mfuesslin, also check in `C:\Windows\System32\bash.exe`. – Druckles Sep 16 '19 at 13:56
19

Since WebStorm 2020.2 EAP it is possible.

Just add \\wsl$\YOUR-WSL-VERSION\usr\bin\git to the Path to Git executable:

enter image description here

To get your WSL VERSION type in a console wsl -l

Gabrielizalo
  • 896
  • 1
  • 17
  • 29
  • 7
    This is fab! Scrolling to the bottom paid off :D – Craig Jun 13 '20 at 15:49
  • when i click test instead of the git version i get a prompt that git isn't installed, with a popup after i click on it to downgrade git for windows to 2.24.1.2 and then git for windows is set as my git again – notacorn Jul 28 '20 at 14:35
  • @notacorn Which version of WebStorm do you have installed? – Gabrielizalo Jul 28 '20 at 22:04
  • i have intellij 2020.1.4 installed so thats probably my problem @Gabrielizalo now waiting on lombok – notacorn Jul 29 '20 at 04:07
  • @notacorn I had the same problem with PyCharm. But I just updated path to Git executable, and with Test button I got an error. I just ignore it. And it works anyway. – Gabrielizalo Jul 29 '20 at 12:49
  • This also works in PhpStorm since [2020.2](https://blog.jetbrains.com/idea/2020/06/intellij-idea-2020-2-eap3-support-for-git-installed-in-wsl2-java-completion-improvements-and-more/) – Philipp Michael Jul 30 '20 at 12:24
  • This also works in IntelliJ IDEA 2020.3 (Ultimate Edition) ! In my case, I had to enter **\wsl$\Ubuntu\usr\bin\git** for the path even though I have _Ubuntu 20.04 LTS_ version installed. – droduit Dec 23 '20 at 15:25
11

In PyCharm 2018.1 I got various errors, when trying to settled up Git. I've to combine different approaches to make it run. Next code works for me:

@echo off
setlocal enabledelayedexpansion
set command=%*
If %PROCESSOR_ARCHITECTURE% == x86 (
    echo | C:\Windows\sysnative\bash.exe -c 'git %command%'
) Else (
    echo | bash.exe -c 'git %command%'
)

UPD:

Now is available integration with Git inside WSL through WSLGit wrapper. I've checket it out with PyCharm and it's work like a charm. Here is a link https://github.com/andy-5/wslgit

eirenikos
  • 2,296
  • 25
  • 25
  • https://github.com/andy-5/wslgit worked for me on with WSL 2 and PhpStorm 2019.3. Thanks for that. – nwaweru Feb 19 '20 at 06:38
6

Change the double to single quotes.

You can log, what arguments are fed to your bat file

@echo off
@echo %*>> %~dp0log.txt
bash.exe -c 'git %*'

With that, i discovered i had some escaping problems.

FYI: With the Win10 creators update piping bash and spawning it from Windows programs works fine.

KatoPue
  • 61
  • 2
4

In PhpStorm (2017.2 EAP) I get error

Caused by: com.intellij.openapi.vcs.VcsException: 'bash.exe' is not recognized as an internal or external command, operable program or batch file.

For solution i change last line to

If %PROCESSOR_ARCHITECTURE% == x86 (
    C:\Windows\sysnative\bash.exe -c 'git %command%'
) Else (
    bash.exe -c 'git %command%'
)
XeuRun
  • 41
  • 5
3

For me this solution works:

File: git.bat

@echo off
setlocal enabledelayedexpansion
set command=%*
If %PROCESSOR_ARCHITECTURE% == x86 (
    C:\Windows\sysnative\bash.exe -c 'git %command%'
) Else (
    bash.exe -c 'git %command%'
)
Rdey
  • 420
  • 6
  • 9
3

As Gabrielizalo answered earlier, you need to use version 2020.2 and higher.

  • Go to Settings | Version Control | Git

  • Add \\wsl$\YOUR-WSL-VERSION\usr\bin\git to the Path to Git executable

  • Press Test

Please note, if you are using the WLinux distribution, you need to use the name Pengwin. Even though the wsl -l command outputs the name as WLinux. Perhaps It will be fixed in future versions.

This is how it works for me \\wsl$\Pengwin\usr\bin\git.

If you are still having problems with Pengwin (WLinux). You need to reimport it as Pengwin. Follow this instruction:

  • Restart your system.
  • Open PowerShell as administrator.
  • wsl --list. You will see a list of distributions, including WLinux (Default).
  • Make a backup with command wsl --export distro_name file_name.tar.
    For example, wsl --export WLinux E:\backup.tar. It will take some time, as the distribution can reach several gigabytes.
  • Make sure the backup is complete, then remove the distribution from WSL with the command wsl --unregister WLinux.
  • Next, reimport the distribution with name Pengwin
    wsl --import distro_name install_location file_name.tar, e.g.,
    wsl --import Pengwin C:\Users\<USERNAME>\pengwin E:\backup.tar
  • Set the distribution as default wsl --setdefault Pengwin
  • To run default WSL distro as the specified user use wsl --user <Username>
  • Add \\wsl$\Pengwin\usr\bin\git to the Path to Git executable to your IDE, and press Test

Note, now when using pengwin in cmd, or from the Start menu, a new Pengwin instance will be installed. You can check this with the wsl --list command. The new instance will show up as WLinux. If you accidentally create a new instance, you can delete it with the command wsl --unregister WLinux.

For the convenience of using your distro, I strongly recommend installing a custom terminal. You can follow this guide How to setup a nice looking terminal with WSL in Windows 10.

innomerphey
  • 191
  • 2
  • 5
  • 1
    When using wsl1, you need to `Enable the git.allow.wsl1.executables option in IDE Registry (Help - FInd Action - Registry...)` as https://youtrack.jetbrains.com/issue/IDEA-242469 does – Halcao Aug 27 '20 at 11:07
2

Worked till PHPSTORM 2018.3 (or maybe a Windows Update changed some behavior regarding bash.exe). I am using Ubuntu 18.04 LTS. However, the path of my bash.exe changed - it is no longer in C:\Windows\Sysnative\bash.exe.

To get things working again I modified Elies Lou's wslgit.bat and set new path for bash.exe:

@echo off
setlocal enabledelayedexpansion
set command=%*
set find=C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt
set replace=/mnt/c/Users/%USERNAME%/AppData/Local/Temp/git-commit-msg-.txt
call set command=%%command:!find!=!replace!%%
echo | C:\Windows\System32\bash.exe -c 'git %command%'
koseduhemak
  • 523
  • 2
  • 4
  • 19
2

I updated the soultion to work with WSL2 with a network drive and PhpStorm 2019.2.

wsl_git.bat:

@echo off
setlocal enabledelayedexpansion
set command=%*
set find=C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt
set replace=/mnt/c/Users/%USERNAME%/AppData/Local/Temp/git-commit-msg-.txt
call set command=%%command:!find!=!replace!%%
echo | wsl CURDIR="%cd%"; STR2=${CURDIR//\\//}; STR3=${STR2/U:/}; cd $STR3; git %command%
  1. It replaces the path in command for git-commit-msg-.txt to be able to commit as it was mentioned in other answers.

  2. With WSL2 I use the network drive: \\wsl$\<distro_name> -> U:\. My project has path on Windows: U:\home\roman\projects\experiments, but on Linux it is /home/roman/projects/experiments. PhpStorm uses path from Windows to work with git, so it is needed to change path which can be reachable in the Linux subsystem. To achieve this I replace slashes \ -> / (STR2) and remove drive name U: -> `` (STR3) then change current dir to this modified path.

rNix
  • 2,457
  • 1
  • 22
  • 28
0
  1. Install your project file in the wsl file system , any way you'll need this iff you go docker ;p
  2. Open your project from \wsl$
  3. (if needed) Go to Settings > Version Control > Git > and simply clear the Path to Git executable

and this work so fine !

tooy
  • 257
  • 2
  • 7
0

Since updating to 2022.3.3 I get this for my \\wsl$\Ubuntu\usr\bin\git path. Which previously worked, and I've also seen it today even work sometimes but not other times.

Git is not installed
Cannot identify version of git executable: no response

It does take a while for my wsl to startup/respond when I open a new terminal for it for example, so maybe the timeout needs to be extended?

Liam Mitchell
  • 1,001
  • 13
  • 23