Skip to content

Instantly share code, notes, and snippets.

@nickbudi
Last active November 4, 2023 10:53
Show Gist options
  • Save nickbudi/4b489f50086db805ea0f3864aa93a9f8 to your computer and use it in GitHub Desktop.
Save nickbudi/4b489f50086db805ea0f3864aa93a9f8 to your computer and use it in GitHub Desktop.
Cygwin git compatibility with VS Code (or other Windows programs) using cygpath

Cygwin Git + VS Code compatibility

Thanks and credit to mattn and ferreus on GitHub.

Also check out Developing on WSL and/or wslpath (Windows 10 Build 17046 or later) if you're using the Windows Subsystem for Linux.

#!/bin/bash
# wrapper to convert linux paths to windows
# so vscode will work as a git editor with cygwin
# editor="/home/this/file.sh" in .gitconfig
# extract last argument (the file path)
for last; do true; done
# get all the initial command arguments
all="${@:1:$(($#-1))}"
# launch editor with windows path
code $all $(cygpath -w $last)
@echo off
REM wrapper to convert linux paths to windows
REM so vscode git integration will work with cygwin
REM "git.path"="C:\\this\\file.bat" in settings.json
setlocal
set PATH=C:\cygwin\bin;%PATH%
if "%1" equ "rev-parse" goto rev_parse
git %*
goto :eof
:rev_parse
for /f %%1 in ('git %*') do cygpath -w %%1
@benyaminl
Copy link

benyaminl commented Mar 2, 2018

I want to ask, why mine not working?
+++++++++++++++++++++++++++++++
Edit, mine work after I remember the cygwin is 64 bit.. what a day.. thanks for create a such code :)

@feryardiant
Copy link

Thanks so much!

@jvolden
Copy link

jvolden commented May 15, 2018

Suggest adding "chmod +x file" to both file comments. I didn't expect I would need it for the .bat file.

These will now live in my dotfiles!
Thanks!

@wheelerlaw
Copy link

wheelerlaw commented May 24, 2018

Hmm, doesn't seem to be working for me. I keep getting this error (in the VSCode git log):

git rev-parse --show-toplevel
fatal: not a git repository (or any of the parent directories): .git

But if I run the command from the command line, it works fine:

C:\openshift_templates>"C:/cygwin64/bin/cygpath-git-vscode.bat" rev-parse --show-toplevel
C:\openshift_templates

Maybe a VSCode issue?

Edit, added full log when I try to Discard Changes:

Looking for git in: C:/cygwin64/bin/cygpath-git-vscode.bat
Using git 2.17.0 from C:/cygwin64/bin/cygpath-git-vscode.bat
> git rev-parse --show-toplevel
> git config --get commit.template
Open repository: c:\openshift_templates
> git status -z -u
> git symbolic-ref --short HEAD
> git show :build.gradle
> git rev-parse master
> git check-ignore -z --stdin
> git rev-parse --symbolic-full-name master@{u}
fatal: ambiguous argument 'master@u': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose
> git checkout -q -- c:\openshift_templates\build.gradle
error: pathspec 'c:\openshift_templates\build.gradle' did not match any file(s) known to git.
> git status -z -u
> git symbolic-ref --short HEAD
> git rev-parse master
> git rev-parse --symbolic-full-name master@{u}
fatal: ambiguous argument 'master@u': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose

@ewilazarus
Copy link

Thanks a lot! Works like a charm.

@Egor-Koldasov
Copy link

Works great, thanks

@abdalmoez
Copy link

Works for me!!!
In cygpath-git-vscode.bat
change set PATH=C:\cygwin\bin;%PATH% to set PATH=C:\cygwin64\bin;%PATH% for 64bit cygwin

@musm
Copy link

musm commented Jul 4, 2020

sadly not working if I try to use this script as GIT_EDITOR

@musm
Copy link

musm commented Jul 4, 2020

Actually this line needs to be set to

code $all $(cygpath -w $last)

and also change

export GIT_EDITOR="$HOME/cygpath-git-editor.sh -w"

@ubw
Copy link

ubw commented Apr 23, 2021

works for me! thank you!

@DanKaplanSES
Copy link

DanKaplanSES commented Sep 27, 2021

Thank you for sharing this! I have provided some step-by-step instructions here: https://stackoverflow.com/a/69340165/61624

@StLeoX
Copy link

StLeoX commented Mar 28, 2022

Emmm...There may be still a small problom for me. In this way, it's display file status correctly. But when using the source manage in the VSCode Sidebar, it doesn't work. It still prompt "can't spawn git.bat". So sad :(

@gene-pavlovsky
Copy link

I tried the bat wrapper, it worked for basic git status display, but trying to stage/unstage a file resulted in errors.

I.e. either of these commands issued by VS Code failed:
Stage: git add -A -- C:\project\hxcalc\package.json
Unstage: git reset -q HEAD -- C:\project\hxcalc\package.json
Both of them result result in fatal: C:\project\hxcalc\package.json: 'C:\project\hxcalc\package.json' is outside repository at '/mnt/c/project/hxcalc'

I tried the same commands in Cygwin bash, and indeed, Cygwin git doesn't seem to be able to handle Windows paths.

What worked for me is this: github.com/nukata/cyg-git. It's a tiny C wrapper that you compile with gcc (for my use case, regular Cygwin gcc worked), and a Zsh wrapper script that works similar to cygpath-git-vscode.bat, but it also takes care of converting paths passed to git. I had to make a small change to this script to make it handle rev-parse commands that produce multiple lines of output (made a PR to the original repo).

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