obsidian-git
obsidian-git copied to clipboard
[Bug]: custom Git binary path using wsl doesn't work
[note: suspected cause at the very end; skip ahead and come back if that's not it]
I have Git installed in WSL and added a shell script in my home that will take care of connecting to the ssh-agent etc. followed by actually running the requested git command. Despite the command working in both PowerShell and plain CMD, Obsidian Git reports "cannot run git command".
- Are you sure that the custom git binary path code actually works?
- If yes, what's the required format?
I have tried both C:\Windows\System32\wsl.exe /home/REDACTED/bin/ogit as well as just wsl.exe /home/REDACTED/bin/ogit or wsl /home/REDACTED/bin/ogit (in case there's a problem with handling backslashes...)
In CMD and PowerShell, both work:
C:\Users\REDACTED\Desktop\maps\obsidian>wsl /home/REDACTED/bin/ogit status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .obsidian/plugins/obsidian-git/data.json
C:\Users\REDACTED\Desktop\maps\obsidian>C:\Windows\System32\wsl.exe /home/REDACTED/bin/ogit status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .obsidian/plugins/obsidian-git/data.json
C:\Users\REDACTED\Desktop\maps\obsidian>C:\Windows\System32\wsl.exe /home/REDACTED/bin/ogit push
Host key fingerprint is SHA256:<redacted>
Enumerating objects: 38, done.
Counting objects: 100% (38/38), done.
Delta compression using up to 8 threads
Compressing objects: 100% (27/27), done.
Writing objects: 100% (29/29), 6.13 KiB | 184.00 KiB/s, done.
Total 29 (delta 4), reused 0 (delta 0)
To <redacted>:repos/obsidian-vault.git
<redacted>..<redacted> master -> master
In Obsidian, all variants fail.
So there seems to be some special formatting / adjustment to the command required to make it work when run from Obsidian. (Or maybe it just doesn't work at all?)
While I don't really understand JS & related languages, from a quick scan of the code in src/simpleGit.ts, the following seem strange:
isGitInstalled,commitAll, andpushall appear to hard-code thegitcommand; ifisGitInstalledgatekeeps the running of all other commands, then it's not surprising that nothing works
I haven't tested it with wsl. I guess the problem is that you first run wsl and then the git path for wsl, but that's caused by the underlying git library. Seems to be the same issue as in this issue
Regarding your guess why it doesn't work, the command use the git object, which is created here with the path from the settings.
But isGitInstalled just runs
spawnSync('git', ['--version'], …) and doesn't use the indirection, right? (The code you linked is only reached if isGitInstalled returns success, so it can't have been reached yet.)
And since the indirection is required to find git (as it's not just git), of course it can't work.
Oh ** you are right. Definitely need to fix that. But I still doubt that it works, because the user in the other issue had a local git installation and just wanted to use the wsl installation, which didn't work. Maybe your setup is different and it works nevertheless.
Released a new version, please try it out.
Progress! After the update, I'm getting /bin/bash: status: command not found which is exactly the error message that wsl status generates.
So it looks like something drops the second part of wsl /home/REDACTED/bin/ogit, and I confirmed that with an experiment. (Details below.)
For now, I added an extra git.cmd wrapper somewhere in the %PATH% and then wondered why it doesn't work until I found out that apparently I need to add the full C:\... path in spite of it being in %PATH% (where both cmd and powershell find & run it without problems). So now things mostly work for me!
I'd love to see the argument-dropping problem fixed eventually (so I no longer need the second wrapper) but it's not urgent.
Experiment: To test the argument dropping, I added symlinks named status, branch, ... in /usr/local/bin to another wrapper that calls /home/REDACTED/bin/ogit "$(basename "$0")" "$@" (i.e. reroutes those program names to become a git command), such that wsl status, wsl branch, ... will work like wsl /home/REDACTED/bin/ogit status, .... and that confirms this guess: With that in place, I can select the branch from the drop-down in the obsidian-git config and I get a "git: ready" in the status bar, i.e. basic communication with git works. (Adding lots of other wrappers for add/commit/push/... means I can use the source control view to add, commit, push/pull and it all works, as verified externally.)
Just for reference, in case others want my full solution for getting WSL's git working: There's a git.cmd
@echo off
C:\Windows\System32\wsl.exe /home/REDACTED/bin/obsidian-git %*
and since %PATH% search doesn't work anyway, just place that anywhere and specify the full path to it as the custom command. On the Linux side, there's the obsidian-git wrapper that connects to the running ssh-agent and then calls git:
#!/bin/sh
source ${HOME}/.profile > /dev/null 2>&1 # this is where my config does the ssh-agent connecting
git "$@"
and in case you don't have an automated ssh-agent setup yet, eval "$(keychain --eval --agents ssh)" in e.g. your .profile with keychain installed gets that done without lots of duplicate ssh-agents. (Just ssh-add the key once after boot to unlock/cache it and you're done.)
(Also I guess @maximberezin97 might want to know that there was indeed a problem and it mostly works now. Oh and thanks for the quick help too, Vinzent!)
Sorry to hear that it still doesn't work. I'm just providing the path to the simple-git library, so it has to be an issue on their side.
I'm really thankful for your workarounds for both Windows and Linux! Should help other people facing the same issue.
Progress! After the update, I'm getting
/bin/bash: status: command not foundwhich is exactly the error message thatwsl statusgenerates.So it looks like something drops the second part of
wsl /home/REDACTED/bin/ogit, and I confirmed that with an experiment. (Details below.)For now, I added an extra
git.cmdwrapper somewhere in the%PATH%and then wondered why it doesn't work until I found out that apparently I need to add the fullC:\...path in spite of it being in%PATH%(where both cmd and powershell find & run it without problems). So now things mostly work for me!I'd love to see the argument-dropping problem fixed eventually (so I no longer need the second wrapper) but it's not urgent.
Experiment: To test the argument dropping, I added symlinks named
status,branch, ... in/usr/local/binto another wrapper that calls/home/REDACTED/bin/ogit "$(basename "$0")" "$@"(i.e. reroutes those program names to become a git command), such thatwsl status,wsl branch, ... will work likewsl /home/REDACTED/bin/ogit status, .... and that confirms this guess: With that in place, I can select the branch from the drop-down in the obsidian-git config and I get a "git: ready" in the status bar, i.e. basic communication with git works. (Adding lots of other wrappers foradd/commit/push/... means I can use the source control view to add, commit, push/pull and it all works, as verified externally.)Just for reference, in case others want my full solution for getting WSL's git working: There's a
git.cmd@echo off C:\Windows\System32\wsl.exe /home/REDACTED/bin/obsidian-git %*and since
%PATH%search doesn't work anyway, just place that anywhere and specify the full path to it as the custom command. On the Linux side, there's theobsidian-gitwrapper that connects to the runningssh-agentand then calls git:#!/bin/sh source ${HOME}/.profile > /dev/null 2>&1 # this is where my config does the ssh-agent connecting git "$@"and in case you don't have an automated
ssh-agentsetup yet,eval "$(keychain --eval --agents ssh)"in e.g. your.profilewithkeychaininstalled gets that done without lots of duplicatessh-agents. (Justssh-addthe key once after boot to unlock/cache it and you're done.)
Thank you so much @229c9cf0 , this solution worked for me! I created the two helper files git.cmd and the wrapper script obsidian-git and set my custom Git path to C:\Users\MyUser\git.cmd, and had finally been able to push and pull from my Obsidian repository!

The hardest part was getting my ssh-agent to run automatically on login, but the ssh-agent plugin for Oh My Zsh takes care of this in one line change in my .zshrc. While this solution does appear quite fragile, it is working for the time being. I really appreciate both of your help.
I am trying to get git in WSL working but I keep getting an error
plugin:obsidian-git:26622 Uncaught (in promise) Error: Error: Git.cwd: cannot change to non-directory "/mnt/c/Users/FirstName LastName/obsidian-vault"
at GitExecutorChain.onFatalException (plugin:obsidian-git:26622:79)
at GitExecutorChain.eval (plugin:obsidian-git:26614:24)
at Generator.throw (<anonymous>)
at rejected (plugin:obsidian-git:25483:29)
at process.processTicksAndRejections (node:internal/process/task_queues:96:5).
I setup a git.cmd and wrapper as git-wrapper. Both of these files are located in my vault.
The contents git.cmd is:
@echo off
C:\Windows\System32\wsl.exe /mnt/c/Users/FirstName\ LastName/obsidian-vault/git-wrapper %*
and git-wrapper contains
#!/bin/sh
source ${HOME}/.profile > /dev/null 2>&1
git "$@"
From the command prompt in windows I can pass git commands through git.cmd and the same with the WSL ubuntu terminal using git-wrapper
I have the "Custom Git binary path" set to:
C:\Users\FirstName LastName\obsidian-vault\git.cmd
In the dev-console I have run app.vault.adapter.basePath which returns `'C:\Users\FirstName LastName\obsidian-vault'
Does anyone have any ideas? The only thing I can think is the space between first and lastname in my windows Users directory is causing issues.
EDIT: I applied the changes from #587 and I was able to make progress, but I got another error:
plugin:obsidian-git:29119 Uncaught (in promise) Error: 'C:\Users\FirstName' is not recognized as an internal or external command, operable program or batch file.
I moved git.cmd to c:\tools\git.cmd and everything is working. Is there a proper way to format paths for the git binary if a space is part of the name?
@raulvasquez You are using the git.cmd as Git Binary Path, right? Can you wrap it in ' in the settings? That should fix the whitespace in the path issue.
It cannot run the git command when I surround it in '. I've also tried double quotes " and escaping the space.
Hmm I can't get it to work as well. Seems like a bug in simpel-git to me. You'll have to use no space then.
Haven't used Obsidian in a while, but I've looked into the current iteration of problems as well now.
When checking validity of git settings, the plugin currently runs git -c core.quotepath=off rev-parse --show-toplevel, which results in a path of the shape /mnt/c/<…normal windows path…>. It doesn't log what it's trying to do next, but whatever that is results in an Error: Git.cwd: cannot change to non-directory /mnt/c/… (because the /mnt/c/ prefix is doesn't make sense on the Windows side.) This does not require whitespace or other special characters in the path components.
Apparently at some point the behavior of the plugin changed from just working with relative paths or passing paths as arguments (which works fine if Obsidian is in "Windows-land" and git lives in "WSL-/Linux-land") to trying to do stuff at the location of the git repository (which breaks since the absolute paths aren't the same.)
I don't have time to dig into the code, and the logs don't contain more information, so that's all I can figure out for now… But maybe that helps someone else.
Found a silly/stupid workaround: (@raulvasquez)
In PowerShell, cd /, mkdir mnt, cd mnt, junction c ...
That creates a folder C:\mnt containing a junction link back up to to C:\, which makes paths of the form /mnt/c/… work. At that point, the current behavior of using absolute paths doesn't hurt anymore.
Just noticed activity on this issue - wanted to put in my 2 cents since I have a working solution that satisfies me.
My motivation: I want Windows Obsidian to use WSL's git for obsidian-git, because my WSL has my ssh keys in the agent and also my git repo has git-crypt set up.
- I have the latest
obsidian-gitpatched with #587 (just updated to latest in my fork) - I have the Custom Git binary path setting point to
C:\Users\<user>\AppData\Local\scripts\wsl_git.cmdI believe I didnt run into the issue described above simply because my Windows user has no spaces. I'd suggest attempting to escape them with\, but can't test that theory. - Contents of
wsl_git.cmd, inspired by this thread:
@echo off
wsl.exe /home/<wsl user>/bin/obsidian-git %*
- Contents of
~/bin/obsidian-git, which ensures my env is properly set up (most importantly, the correctSSH_AUTH_SOCK):
#!/bin/bash
source ${HOME}/.profile > /dev/null 2>&1
git "$@"
The last one is optional, if you don't need your .profile you can just have wsl_git.cmd pointing to the wsl git binary instead.
With this setup, obsidian-git pulls and pushes to my remote seamlessly using my WSL auth configuration.
Hope this helps!