Obsidian Git-based Sync on Windows

This post will show you how to synchronize Obsidian using git on a Windows 10 PC, similarly to the way this other post did for Linux/Mac:

Windows 10 Pro may be needed for the below. But it might work on Home too.

I assume below you have a basic understanding of git (can create a repo, know how to commit and push/pull)

Basics

I used the Task Scheduler together with a PowerShell script that commits and pushes the changes to the remote repository.

The Task Scheduler can be accessed by pressing the start button and typing task, it should come right up.

This is the script

$changes = git status --porcelain | Measure-Object -Line
if($changes.Lines -gt 0)
{
    $date = Get-Date
    git add .
    git commit -q -m "$date"
    git push -q 
}

Set up the repository. Use the .gitignore as recommended in the linked post above. Open PowerShell and make sure the script above pushes to your remote (GitHub etc…)as expected. Then move on to scheduling synchronization as instructed below.

For the script to work you need to first launch a PowerShell session as admin and run Set-ExecutionPolicy RemoteSigned . This will let you run script that you have created locally but disallow downloaded ones. Without this command, no script are allowed by default.

You have two options:

  1. (easy) Have task scheduler tun this script at intervals
  2. (harder) Create a custom trigger that fires off the script when Obsidian closes.

Time-based

Go into task scheduler. Create a new folder for your task and put it in there.

In the trigger tab choose Create Task, (not basic task). Have the task start on login and repeat as often as you think appropriate.

In the action tab Choose start a program from the drop down. Choose PowerShell.exe for the program with the argument as the path to the script. Start in should be set to the Obsidian Folder. You can put the script there, that is fine.

All other options can be left as their defaults.

Process-Close-Event based

The problem with a time based trigger is that it need to be running often or it might not sync before you log off, etc…

Open Local Security Policy (Start button and begin typing it…). When it opens navigate to Advanced Audit …-> System Audit … → Detailed Tracking and in the right hand window set Audit Process Termination Configure it to on/success.

Now back to the Task Scheduler. Change the trigger to beign the Task On an event Custom, Edit event filter and in the XML put

<QueryList>
  <Query Id="0" Path="Secuity">
    <Select Path="Security">*[System[(EventID=4689)]] and *[EventData[Data[@Name='ProcessName'] and (Data='C:\path\to\Obsidian\Obsidian.exe')]]</Select>
  </Query>
</QueryList>

Replace the path to you Obsidian executable.

Let me know if you have any problems!

4 Likes

There is also a plugin for this.

And which one would that be? Link? Name?

Can you provide example location for where said PS script is supposed to reside?

1 Like