Pycharm using WSL's git?

Answered

I want to configure my PyCharm to use the stock Ubuntu git available under the WSL, however I can't find a way to do so. I know there is a git version for Windows, however parts of my codebase require a linux environment and its git, so I'd appreciate having a single git installation in my system. I know I can configure a remote interpreter for Python, but I'm interested in git right now.

The WSL available as part of the Creator's Update allows interoperability between Windows and Linux programs (more here). For example, I can both call Ubuntu's git from a powershell(x64), as well as notepad.exe from bash. Despite this, I can't figure out how to tell PyCharm to use the correct git.

2
15 comments

Maybe I'm a bit late to the party, but in case anyone is wondering, I've found a solution that works (at least for me):

First, create a new directory - for example: "C:\Users\yourname\AppData\Roaming\bin". Then add a file "git.cmd" into this directory and put the following into the file:

@echo off
%WINDIR%\System32\bash.exe -c "git %*"

Last, but not least, add the newly created directory to the PATH. Press Windows+Pause and klick Advanced System Settings on the left. In the window that opens, klick Environment Variables at the very bottom and then select PATH from the upper of the two Lists. Klick Edit and put the path of the created directory in there. Save, reopen PhpStorm and put the full path to the file (in this case "C:\Users\yourname\AppData\Roaming\bin\git.cmd" into the Git Executable field in the PhpStorm settings.

That's it. And now, you can also use WSL's git directly in CMD.

Please note, that, with this approach, PhpStorm cannot show you a password prompt for your SSH Key or Repository Username. To get around this, you must add your keys to WSL's SSH Agent.

13

Forgot to mention: You can do this for almost any WSL binary. Even sudo.

5

I don't think it is possible. WSL is still not providing full integration. Ubuntu git is not a win32 application. To run it through powershell you need to wrap it to bash.exe to catch the output.

IDE does not support specifying "bash.exe -c git" as a git executable.

0

I use [wslgit](https://github.com/andy-5/wslgit)

But the point is - do I need to find/create a wrapper or a hack for every single tool I want to use from WSL, or can Intellij help me with this?

 

0

Sorry to necro but bash.exe will likely run git as root, you may want wsl --user bob git %*, assuming your typical wsl user is not root

0

PyCharm, as all other IntelliJ IDEs, supports WSL git since 2020.2 - see https://youtrack.jetbrains.com/issue/IDEA-172253

To make it work, you can choose the git binary from the installed WSL distro directly in the Git Executable field

0

Very nice, thanks guys. Follow up issue: SSH?

My machine is configured to access GitHub via SSH, and within WSL's bash authentication works as expected (git pull, git push, etc). Within PyCharm, the path to the Git executable got auto-detected, points to the one in WSL, and the Test button answers with the Git version from WSL. However, trying to perform git operations results in an error:

Update failed
git@github.com: Permission denied (publickey).
Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

This occurs even when a WSL terminal is open (and so WSL is running, and the SSH agent I set up is also running).
Are there additional settings I'm missing? Running WSL v2.

The Git console log shows it's trying to use:

[project] git -c credential.helper= [...] fetch origin [...]

That line works when copy/pasted into the bash terminal.

 

0

Hello Hfm5

May I ask you to submit a ticket (request)?
And please reproduce the behavior and attach logs to a ticket as per:
https://intellij-support.jetbrains.com/hc/en-us/articles/207241085-Locating-IDE-log-files

 
0

Hi Ruslan,

Submitted a ticket under https://intellij-support.jetbrains.com/hc/en-us/requests/3159027, including (sanitized) logs.

I filed it under "how to", given that it may very well just be a mis-configuration issue and not necessarily a bug.

 

Best,

H

0

Hi all, I'm not able to find the ticket by Hfm5
Is this issue a point of discussion somewhere else please?
Thanks, M

0

Milan Vancl

Git under WSL should work, you can find some discussion here https://youtrack.jetbrains.com/issue/IDEA-172253

If it's still not working for you, I suggest submitting an issue to https://youtrack.jetbrains.com with Logs from **Help | Collect Logs and Diagnostic Data** after reproducing the issue

0

Andrey Resler thank you for your answer, but sorry I mean the issue related to SSH put later at this comment (https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000176290-Pycharm-using-WSL-s-git-?page=1#community_comment_360002794779)

PS: Git binaries works well as you suppose

0

Hi Milan Vancl, I just re-checked and the ticket is available at that link (i.e. https://intellij-support.jetbrains.com/hc/en-us/requests/3159027), but does require signing-in. I however did not keep digging into the issue with PyCharm because I got VisualStudioCode working (it uses the WSL Python, the WSL git, and the WSL SSH keys), and have migrated my workflow to that tool.

0

Hi Hfm5, thanks for your response, but it is strange, I'm literally getting 404 when I click the link.

Anyway, my solution is currently using Pycharm with Git for Windows and Windows OpenSSH Agent. With proper settings of those tools It's fully working with no issues collaborating with other non-Windows developers.

0

Please sign in to leave a comment.