Search code examples
gitwindows-subsystem-for-linuxgitk

gitk: "Cannot find a git repository here" when running in WSL 2


I am trying to run gitk.exe (the program installed with Git for Windows) from WSL 2. If I do this in a repository sitting on my WSL virtual drive, such as ~/path/to/repository, I get the error, "Cannot find a git repository here." However if I'm in a repository outside of the virtual drive, i.e., /mnt/c/path/to/repository, gitk.exe works just fine even though I'm still running it from WSL. How can I get gitk.exe to work on repositories in my WSL filesystem?


Solution

  • I found the problem. According to this answer, gitk runs git under the hood to determine if it's being run in a git repository. So, it's reasonable to think that gitk.exe would run git.exe-that is, the binary installed on Windows, not the binary installed from my package manager in the WSL virtual drive.

    Running git.exe status from my ~/path/to/repository/ resulted in:

    fatal: unsafe repository ('//wsl$/Ubuntu/home/myusername/path/to/repository' is owned by someone else)
    To add an exception for this directory, call:
    
            git config --global --add safe.directory '%(prefix)///wsl$/Ubuntu/home/myusername/path/to/repository'
    

    To fix this I first tried running in WSL

    git.exe config --global --add safe.directory '%(prefix)///wsl%/Ubuntu/home/myusername/path/to/repository'
    

    But for some reason, that did not resolve the error. The next thing I tried was running the same command from a Git Bash window on Windows. After that, running git.exe status from WSL resulted in

    On branch master
    
    nothing to commit, working tree clean
    

    And with that, gitk.exe also worked.

    This answer has some more information about the unsafe repository error.