Using Git in Windows Subsystem for Linux through IntelliJ

﹥>﹥吖頭↗ 提交于 2019-12-02 16:16:05
Elies Lou

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%'

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

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.

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%'
)

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%'
)

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%'

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!