关于批处理文件:通过IntelliJ在Linux的Windows子系统中使用Git

Using Git in Windows Subsystem for Linux through IntelliJ

我试图将IntelliJ中的Git可执行文件设置为安装在Linux的Windows子系统中的git,我尝试了几种不同的方法,但始终会遇到某种错误。 今天,我安装了Creators Update(版本1703),重新安装了WSL,然后再次尝试,这是我所做的:

我创建了一个.bat脚本:

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

因此,在运行它时:

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

因此,然后我尝试在IntelliJ中的git可执行文件上设置此蝙蝠:
Setting Git executable in IntelliJ

而且有效! 但是其他所有操作都失败了,例如,当我尝试在IntelliJ中拉动或分支时,我得到:

1
2
3
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.

有关如何解决此问题的任何想法? 我对批处理脚本一无所知。 它可以在命令行下完美运行。


我一直在寻找一种通过Webstorm或IntelliJ idea软件在Linux的WSL Windows子系统上使用git的方法。

我尝试了KatoPue的解决方案,但出现以下错误:

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

我通过将命令发送到WSL的git时替换路径来解决它

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

wslgit.bat:

1
2
3
4
5
6
7
@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%'


在尝试解决Git时,我在PyCharm 2018.1中遇到了各种错误。我必须结合不同的方法来使其运行。下一个代码对我有用:

1
2
3
4
5
6
7
8
@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:

现在可以通过WSLGit包装器在WSL中与Git集成。我已经通过PyCharm对其进行了检查,它的工作就像是一种魅力。这是一个链接https://github.com/andy-5/wslgit


将双引号更改为单引号。

您可以登录,哪些参数被输入到bat文件中

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

这样,我发现自己遇到了一些逃避的问题。

仅供参考:使用Win10创建者更新管道bash并从Windows程序生成它可以正常工作。


由于WebStorm 2020.2 EAP,所以可以。

只需将\\\\wsl$\\YOUR-WSL-VERSION\\usr\\bin\\git添加到Git可执行文件路径中即可:

enter image description here

要获取WSL VERSION,请在控制台中输入wsl -l


在PhpStorm(2017.2 EAP)中出现错误

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

对于解决方案,我将最后一行更改为

1
2
3
4
5
If %PROCESSOR_ARCHITECTURE% == x86 (
    C:\\Windows\\sysnative\\bash.exe -c 'git %command%'
) Else (
    bash.exe -c 'git %command%'
)

对我来说,此解决方案有效:

档案:git.bat

1
2
3
4
5
6
7
8
@echo off
setlocal enabledelayedexpansion
set command=%*
If %PROCESSOR_ARCHITECTURE% == x86 (
    C:\\Windows\\sysnative\\bash.exe -c 'git %command%'
) Else (
    bash.exe -c 'git %command%'
)

正如Gabrielizalo先前回答的那样,您需要使用2020.2及更高版本。

  • 转到设置|版本控制|吉特

  • \\\\wsl$\\YOUR-WSL-VERSION\\usr\\bin\\git添加到Git可执行文件路径。

请注意,如果使用的是WLinux发行版,则需要使用名称Pengwin。
即使wsl -l命令将名称输出为WLinux。也许它将在将来的版本中修复。

这就是我\\\\wsl$\\Pengwin\\usr\\bin\\git的工作方式。


我更新了魂魄,使其可以通过网络驱动器和PhpStorm 2019.2。与WSL2一起使用。

wsl_git.bat:

1
2
3
4
5
6
7
@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%
  • 它将git-commit-msg-.txt中的路径替换为git-commit-msg-.txt中的路径,以便能够像其他答案中提到的那样进行提交。

  • 对于WSL2,我使用网络驱动器:\\\\wsl$\\-> U:\\。我的项目在Windows上为U:\\home\
    oman\\projects\\experiments
    ,但在Linux上为/home/roman/projects/experiments。 PhpStorm使用Windows的路径来使用git,因此需要更改Linux子系统中可以访问的路径。为此,我替换了斜杠\\-> /(STR2)并删除驱动器名称U:->``(STR3),然后将当前目录更改为此修改路径。


  • 一直工作到PHPSTORM 2018.3(或者Windows Update更改了有关bash.exe的某些行为)。我正在使用Ubuntu 18.04 LTS。但是,我的bash.exe的路径已更改-C:\\Windows\\Sysnative\\bash.exe中不再存在。

    为了使事情再次起作用,我修改了Elies Lou的wslgit.bat并为bash.exe设置了新路径:

    1
    2
    3
    4
    5
    6
    7
    @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%'