Skip to content

Instantly share code, notes, and snippets.

@jmickela
Last active August 14, 2020 14:34
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jmickela/7c383c78af66a37a2446fe7eb733b157 to your computer and use it in GitHub Desktop.
Save jmickela/7c383c78af66a37a2446fe7eb733b157 to your computer and use it in GitHub Desktop.
Trying to get PhpStorm to use git in Bash on Ubuntu on Windows (Windows Subsystem for Linux)
There's a problem that you run into right away: you can't put a command line command, with arguments,
into the path to git executable box.
So putting something like bash.exe -c "git %*" isn't going to work. I wrote a small shell script that
fixes this, for both 32-bit and 64-bit systems.
@echo off
If %PROCESSOR_ARCHITECTURE% == x86 (
"C:\Windows\sysnative\bash.exe" -c "git %*"
) Else (
"bash.exe" -c "git %*"
)
If you set this script as the path to the git executable you'll get the correct git version back when
you hit the test button. You'll be able to use many of the local VCS functions, like viewing diffs or
previous commits. It should be noted that if you're on a 64 bit system you can't simply ignore the x86
portion of the script, even if you run the 64 bit executable of PhpStorm, the VCS handling is 32 bit.
Or at least when this script is actually invoked it will fail if it can't run the 32 bit version.
Everything falls apart when you try to pull or push from a remote. If you watch the log the reason is
obvious: However the handling of private keys and/or passwords works, it doesn't work here. If you
cancel an attemp to fetch updates you'll get a few messages in the log about not being able to
authenticate with private key or password.
If I run my script in a windows command shell it works fine, it prompts me for a password and fetches
updates. For whatever reason that request for a password isn't making it into PhpStorm.
For now, I give up. Really, and I know it's unlikely, but I would like to see WSL supported because
there's a ton of tools in PhpStorm that assume you aren't doing something stupid like running a
different OS within your OS and trying to pipe all your commands into it while NOT doing this in a
VM where you could just run a SSH server.
@iamoverit
Copy link

iamoverit commented Dec 27, 2017

i found that the difference in line terminators when bash outputs on LF symbol Phpstorm wants to be CRLF there
i think replacing all LF to CRLF could help

upd: i tested it, and i was wrong, after replacing all LF to CRLF doesn't help, Phpstorm still doesn't get any effects
git.exe --version and "bash.exe" -c "git --vesion" | FIND ""/V give the same outputs but second doesnt work, dunno how its possible

@colinmollenhour
Copy link

Thanks for sharing! If you add -i flag for bash.exe to make it read your .bashrc file your .bashrc file can hook up to ssh-agent automatically and thus read your SSH keys to avoid requiring password interaction. Here is what I use to "auto-connect" to the SSH agent:

# Automatically setup ssh agent if flag file exists
if ([[ $- == *i* ]] || [[ -n $CYGWIN_DIR ]]) && [[ -f ~/.ssh/.auto-agent ]]; then
  # Allow cygwin to use PuTTY's pageant
  if [[ -f /usr/bin/ssh-pageant ]]; then
    eval $(/usr/bin/ssh-pageant -ra $TEMP/.ssh-pageant)
    echo "Using PuTTY's Pageant"
  # Use Linux ssh-agent and do not use Gnome Keyring
  elif [[ -f /usr/bin/ssh-agent ]] && [[ $SSH_AUTH_SOCK != /tmp/ssh-* ]]; then
    for agent in /tmp/ssh-*/agent.*; do
      export SSH_AUTH_SOCK=$agent
      ssh-add -l 2>&1 >/dev/null
      if [[ $? -lt 2 ]]; then
        echo "Using already running ssh-agent"
        break
      else
        unset SSH_AUTH_SOCK
      fi
    done
    if [ -z "$SSH_AUTH_SOCK" ]; then
      eval $(ssh-agent)
      echo "Starting new ssh-agent"
    fi
  fi
fi

@oldamalec
Copy link

For anyone still having problems with newest Windows (as of February 2018), the following has worked for me:

@echo off
setlocal enabledelayedexpansion
set command=%*
set find=C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt
set replace=/../../../Users/%USERNAME%/AppData/Local/Temp/git-commit-msg-.txt
call set command=%%command:!find!=!replace!%%
If %PROCESSOR_ARCHITECTURE% == x86 (
    echo C:\Windows\sysnative\bash.exe -c 'git %command%'
) Else (
    echo bash.exe -c 'git %command%'
)

source: this + StackOverflow

@PierBusDev
Copy link

@oldamalec Just wanted to add that after the Creators/Anniversary update bash.exe is deprecated and instead it is suggested the use of wsl.exe which doesn't need the -c option to run a single command

source

@sidx1024
Copy link

sidx1024 commented Jul 21, 2018

Used this in IntelliJ IDEA, seems to work fine.

@echo off
setlocal enabledelayedexpansion
set command=%*

if exist "C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt" (
for /f "delims=" %%i in ('type C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt') do set commitmsg=!content! %%i
)

set find=-F C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt
set replace=-m "%commitmsg%"

call set command=%%command:!find!=!replace!%%
If %PROCESSOR_ARCHITECTURE% == x86 (
    C:\Windows\Sysnative\bash.exe -c 'git %command%'
) Else (
    C:\Windows\System32\bash -c 'git %command%'
)

Instead of letting it use a temp file, I read the commit file into a variable and then commit with "git -m "Commit Message""

@deepaksp
Copy link

deepaksp commented Aug 4, 2018

@echo off

If %PROCESSOR_ARCHITECTURE% == x86 (
C:\Windows\sysnative\wsl.exe git %*
) Else (
wsl git %*
)

this will work :P

@SvenZhao
Copy link

Used this in IntelliJ IDEA, seems to work fine.

@echo off
setlocal enabledelayedexpansion
set command=%*

if exist "C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt" (
for /f "delims=" %%i in ('type C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt') do set commitmsg=!content! %%i
)

set find=-F C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt
set replace=-m "%commitmsg%"

call set command=%%command:!find!=!replace!%%
If %PROCESSOR_ARCHITECTURE% == x86 (
    C:\Windows\Sysnative\bash.exe -c 'git %command%'
) Else (
    C:\Windows\System32\bash -c 'git %command%'
)

Instead of letting it use a temp file, I read the commit file into a variable and then commit with "git -m "Commit Message""

cool

@SvenZhao
Copy link

@echo off

If %PROCESSOR_ARCHITECTURE% == x86 (
C:\Windows\sysnative\wsl.exe git %*
) Else (
wsl git %*
)

this will work :P

cool

@baldrs
Copy link

baldrs commented Nov 17, 2018

@echo off

If %PROCESSOR_ARCHITECTURE% == x86 (
C:\Windows\sysnative\wsl.exe git %*
) Else (
wsl git %*
)

This makes my IDE to hang

@chx
Copy link

chx commented Nov 22, 2018

It works here. Save

@echo off
If %PROCESSOR_ARCHITECTURE% == x86 (
	C:\Windows\sysnative\wsl.exe git %*
) Else (
	wsl git %*
)

in wslgit.bat and point phpstorm to it.

@nielsboecker
Copy link

Used this snippet works for me 😂

@echo off
If %PROCESSOR_ARCHITECTURE% == x86 (
	C:\Windows\sysnative\wsl.exe git %*
) Else (
	wsl git %*
)

Thanks, works for me as well.

@KonradAdamczyk
Copy link

KonradAdamczyk commented Nov 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment