456 lines
13 KiB
Markdown
456 lines
13 KiB
Markdown
|
||
Syncing your obsidian to version control is probably the most efficient way to host your information locally without relying on a cloud provider for unlimited storage. View the process below to see how I got started.
|
||
|
||
>**Git manual update command Shortcut:**
|
||
```
|
||
git add .
|
||
git fetch
|
||
git commit -m "Noted: `date +'%Y-%m-%d %H:%M:%S'`"
|
||
git push -u origin main
|
||
```
|
||
|
||
|
||
|
||
---
|
||
|
||
## Syncing my Obsidian: The Steps I took
|
||
|
||
My Obsidian files are all stored within Google Drive. So in order to maintain sync while still preserving cloud storage space, I used a few different steps than the guides, and well really it was a culmination of it all.
|
||
|
||
---
|
||
|
||
|
||
|
||
##### CURRENT METHOD: Syncing via Gitea Server
|
||
|
||
This can be done locally or via Google Drive. However, I will be using **[Linode](obsidian://open?vault=enter&file=Coding%20Tips%20(Classical)%2FTerminal%20Tips%2FGUIs%2FInternet%2FServers%2FCloud%20Servers%2FLinode)** to host [Gitea](obsidian://open?vault=enter&file=Coding%20Tips%20(Classical)%2FTerminal%20Tips%2FGUIs%2FInternet%2FRepos%2FGitea) to host my obsidian and all my repos locally.
|
||
|
||
But basically this is the self-hosted information flow: A Sketch
|
||
![[Pasted image 20230716192907.png]]
|
||
|
||
|
||
##### 1. **Setting Up Git repo on your local machine.**
|
||
Go to the folder on your laptop that you'd like to start the git syncing your Obsidian Vault at and run:
|
||
```
|
||
git init
|
||
```
|
||
|
||
This sets up the git process. Next you have to clone the existing repository and pull .
|
||
|
||
```
|
||
git clone http:/giteaurlhere.git
|
||
```
|
||
|
||
And then pull the existing information from the repo.
|
||
|
||
```
|
||
git pull
|
||
git add .
|
||
git commit -m "some message"
|
||
git push -u origin main
|
||
```
|
||
|
||
##### 2. Opening Your Vault in that location
|
||
Then Open Your Obsidian Vault there. You are now ready to have your changes synced from Obsidian to Git.
|
||
|
||
```
|
||
git add .
|
||
git commit -m "Updated: `date +'%Y-%m-%d %H:%M:%S'`"
|
||
git push -u origin main --force
|
||
```
|
||
Push your changes once more after you have opened your vault there.
|
||
|
||
Every time you open your git now:
|
||
```
|
||
git pull #make sure you do this to sync your changes
|
||
git add .
|
||
git commit -m "Updated: `date +'%Y-%m-%d %H:%M:%S'`"
|
||
git push -u origin main
|
||
```
|
||
|
||
##### 3. Syncing Your Vault to Push every few minutes
|
||
So this is all great and all, you can now push and pull to a remote server that you own! However, it is a bit annoying to have to do the same terminal commands of git pull, add, commit, push, blah blah dsjkhfalfhk. It can get repetitive. So what do we do? We make a script that can take care of this automatically for us.
|
||
|
||
|
||
|
||
###### *For Linux:* Sync the Obsidian vault very few minutes with this script via `cron`:
|
||
|
||
|
||
```shell
|
||
#!/usr/bin/env sh
|
||
# ^^^^^^^^^^^^^^^ This says find the first instance of a sh (shell)
|
||
# binary and use that shell to execute these commands.
|
||
# There is little to no complexity here and no bashisms so it
|
||
# should work just fine on most systems and instances of shells
|
||
# (bash, zsh, sh, etc.)
|
||
|
||
ZK_PATH="PATH TO YOUR VAULT"
|
||
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ We are assigning the variable `ZK_PATH`
|
||
# with the (maybe) long string to our vault's location (mine is super
|
||
# long so this makes the final command look cleaner,
|
||
# it's unnecessary if you care)
|
||
|
||
cd "$ZK_PATH"
|
||
# ^^^^^^^^^^^ cd: Change Directory to your vault's location
|
||
|
||
git pull
|
||
# ^^^^^^ So if any changes occurred remotely or on another machine
|
||
# your local machine knows to pull those changes down instead of
|
||
# having to wait for a local change to run the script
|
||
|
||
CHANGES_EXIST="$(git status --porcelain | wc -l)"
|
||
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ we are assigning
|
||
# a value to the variable `CHANGES_EXIST`, the value is the output
|
||
# of `git add --porcelain` which outputs a simple list of just the
|
||
# changed files and then the output is piped into the `wc` utility
|
||
# which is "word count" but with the `-l` flag it will count lines.
|
||
# basically, it says how many total files have been modified.
|
||
# if there are no changes the output is 0
|
||
|
||
if [ "$CHANGES_EXIST" -eq 0 ]; then
|
||
exit 0
|
||
fi
|
||
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The whole if block is saying
|
||
# in plain english: if there are no changes (CHANGES_EXIST = 0)
|
||
# then exit with no error code `exit 0` if there are changes,
|
||
# then continue on with the script
|
||
|
||
git pull
|
||
# ^^^^^^ git pull: this will look at your repo and say "any changes?"
|
||
# if there are they will be brought down and applied to your local machine
|
||
# In the context of a team environment, a more robust approach is needed
|
||
# as this workflow doesnt factor in branches, merge conflicts, etc
|
||
# but if you leave your home machine, do work on the work machine,
|
||
# push to the remote repo before you return to the home machine, then
|
||
# you can just get the latest changes applied to the home machine and
|
||
# continue on like normal
|
||
git add .
|
||
# ^^^^^^^ git add. = add all current changes in the repo no
|
||
# matter the level of nested folders/files
|
||
git commit -q -m "Last Sync: $(date +"%Y-%m-%d %H:%M:%S")"
|
||
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||
# git commit -q -m: this says we are committing changes to
|
||
# our repo, -q says BE QUIET no output prints to terminal
|
||
# if ran manually, -m defines a message for the commit log
|
||
# the -m message is "Last Sync: $(date +"%Y-%m-%d %H:%M:%S")" this
|
||
# runs the command date with the formatting arguments for a
|
||
# date in YYYY-MM-DD HH-MM-SS format as your commit message
|
||
git push -q
|
||
# ^^^^^^^^^ git push -q: push the changes to github and
|
||
# BE QUIET about it The semicolons between commands are
|
||
# just saying run each command and then run the subsequent
|
||
# command, they're just separators
|
||
|
||
Save
|
||
|
||
```
|
||
|
||
---
|
||
|
||
|
||
|
||
###### *For Mac:* Sync Obsidian vault via `launchd` by writing a plist script:
|
||
|
||
1. We will be using launchd. Check your list of services by:
|
||
```
|
||
sudo launctl list
|
||
```
|
||
|
||
![[Pasted image 20230714193655.png]]
|
||
It sort of looks like that!^
|
||
So the goal here is to put a launchd .plist into that so that your Mac knows to trigger a LaunchDaemon upon a certain trigger.
|
||
|
||
2. First, however we need to make a bash script that does that syncing itself. This can be done by navigating to `~/.local/bin` and create a shell script by:
|
||
|
||
```
|
||
touch zk_sync
|
||
chmod +x zk_sync
|
||
```
|
||
|
||
*Separately:* Also make sure to copy the absolute path of your vault as that will be the environment variable that you will input into `ZK_PATH` to ensure that it still works. You can obtain the path by:
|
||
```
|
||
> pwd
|
||
#output
|
||
> /Users/shwethajayaraj/Google Drive/My Drive/RESOURCES (Research - my notebook)/collabtestdir/Notepad
|
||
```
|
||
If there are spaces in your working directory just make sure that you put in wrapped double quotation marks.
|
||
|
||
3. What are the actual contents of the shell script though that have to be done?? Well, go ahead and `vim` into the `zk_sync` file and paste the following in:
|
||
|
||
|
||
Attempt #3 (this one worked!!):
|
||
```shell
|
||
#!/usr/bin/env sh
|
||
|
||
|
||
export ZK_PATH=/Volumes/GoogleDrive-117209510583853875316/My\ Drive/Resources/collabtestdir/Notepad
|
||
|
||
cd "$ZK_PATH"
|
||
|
||
git fetch
|
||
|
||
CHANGES_EXIST=$(git status — porcelain | wc -l)
|
||
|
||
current="`date +'%Y-%m-%d %H:%M:%S'`"
|
||
|
||
msg="Updated: $current"
|
||
|
||
|
||
if [ “$CHANGES_EXIST” -eq 0 ];
|
||
then
|
||
exit 0
|
||
|
||
|
||
fi
|
||
git add --all;
|
||
git commit -q -m "$msg + $CHANGES_EXIST";
|
||
# Updated: 2019-08-28 10:22:06 + lines changes
|
||
sleep 10 # for testing
|
||
git pull # --rebase is the default
|
||
sleep 10 # for testing
|
||
git push -q
|
||
```
|
||
|
||
Updated `zk_sync` file"
|
||
```bash
|
||
|
||
#!/usr/bin/env sh
|
||
|
||
|
||
|
||
export ZK_PATH=/Volumes/GoogleDrive-117209510583853875316/My\ Drive/Resources/collabtestdir/Notepad
|
||
|
||
cd "$ZK_PATH"
|
||
|
||
git fetch
|
||
|
||
|
||
|
||
CHANGES_EXIST=$(git status — porcelain | wc -l)
|
||
|
||
formatted_date=$(date +"%A, %B %d, %Y, %H:%M:%S")
|
||
|
||
# current="`date + %H:%M:%S'`"
|
||
|
||
|
||
|
||
msg="Updated: $formatted_date"
|
||
|
||
|
||
|
||
if [ “$CHANGES_EXIST” -eq 0 ];
|
||
|
||
then
|
||
|
||
exit 0
|
||
|
||
|
||
|
||
fi
|
||
|
||
git add .
|
||
|
||
git commit -m "$msg"; # Updated: 2019-08-28 10:22:06
|
||
|
||
sleep 10
|
||
|
||
#git pull
|
||
|
||
git push -u origin main
|
||
|
||
```
|
||
|
||
|
||
Attempt #4: (Previous one broke, Current one)
|
||
|
||
```bash
|
||
|
||
#!/usr/bin/env sh
|
||
|
||
|
||
|
||
export ZK_PATH=/Volumes/GoogleDrive-117209510583853875316/My\ Drive/Resources/collabtestdir/Notepad
|
||
|
||
cd "$ZK_PATH"
|
||
|
||
git fetch
|
||
|
||
|
||
|
||
CHANGES_EXIST=$(git status — porcelain | wc -l)
|
||
|
||
formatted_date=$(date +"%A, %B %d, %Y, %H:%M:%S")
|
||
|
||
# current="`date + %H:%M:%S'`"
|
||
|
||
|
||
|
||
msg="Noted: $formatted_date"
|
||
|
||
|
||
|
||
if [ “$CHANGES_EXIST” -eq 0 ];
|
||
|
||
then
|
||
|
||
exit 0
|
||
|
||
|
||
|
||
fi
|
||
|
||
git add .
|
||
|
||
git commit -m "$msg"; # Noted: 2019-08-28 10:22:06
|
||
|
||
sleep 10
|
||
|
||
#git pull
|
||
|
||
git push -u origin main
|
||
|
||
```
|
||
|
||
|
||
|
||
Run the script in the `/.local/bin` directory:
|
||
```
|
||
bash zk_sync
|
||
```
|
||
|
||
and your git should be updated with that one command with the updated time and the number of lines that were changed in the git commit.
|
||
|
||
|
||
Now that we have ensured that the script itself the hard part is over.
|
||
**The next step is to make sure that you are syncing at a consistent time!**
|
||
|
||
|
||
4. Now it is time to submit this to the `launchd` scheduler. Navigate to `~/Library/LaunchAgents` as this is where you will be dropping your .plist file.
|
||
|
||
Then create the .plist which calls the previous `zk_sync` script that we made before by pasting the following:
|
||
```shell
|
||
<?xml version="1.0" encoding="UTF-8"?>
|
||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
|
||
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||
<plist version="1.0">
|
||
<dict>
|
||
<key>Label</key>
|
||
<string>us.shwetha.obsidian</string>
|
||
<key>ProgramArguments</key>
|
||
<array>
|
||
<string>/Users/shwethajayaraj/bin/zk_sync.sh</string>
|
||
</array>
|
||
<key>StartInterval</key>
|
||
<integer>180</integer>
|
||
</dict>
|
||
</plist>
|
||
|
||
```
|
||
note: The 180 integer interval is made in *seconds*, thus this script executes the automation to run every 3 minutes.
|
||
|
||
5. Once you have saved it as a .plist in the right directory, make sure that you test that it works. run:
|
||
```
|
||
launchctl load -w ~/Library/LaunchAgents/us.shwetha.obsidian.plist
|
||
|
||
```
|
||
to load this `launchd` action. This may be in your local user directory as well.
|
||
|
||
6. Now check your gitea repository.
|
||
![[Pasted image 20230715021300.png]]
|
||
Ta-da!! You did it!!! All your work will now be synced to git every few minutes.
|
||
|
||
No need for pesky cloud storage or physical systems anymore. :)
|
||
Enjoy.
|
||
|
||
---
|
||
|
||
###### ~~OLD METHOD: Making a Gitlab Repo in GDrive ~~
|
||
DON'T DO THIS I learned the hard way :)
|
||
1. **Find your Google Drive location and copy the directory path.
|
||
```
|
||
pwd | pbcopy
|
||
```
|
||
|
||
2. Go to your **local** projects folder and `git init` there locally.
|
||
```
|
||
mkdir gitlab_sync
|
||
cd gitlab_sync
|
||
git init
|
||
git branch -m main
|
||
#renamed branch to main insted of master
|
||
#less typing xD
|
||
|
||
```
|
||
|
||
3. Then create a bare clone copy into your copied google drive location.
|
||
```
|
||
git clone --bare . ~user/Google\ Drive/My\ Drive/folder/location.git
|
||
```
|
||
|
||
4. Then go ahead and make a new remote using this git path inside your local directory.
|
||
```
|
||
git remote add NameOfRemote ~user/Google\ Drive/My\ Drive/folder/location.git
|
||
```
|
||
|
||
5. Make some changes to your git repository by adding files and commit those changes.
|
||
```
|
||
**git** add LICENSE.md
|
||
**git** commit -m 'Initial sync version'
|
||
```
|
||
|
||
6. Then I pushed the changes to the remote google drive location.
|
||
```
|
||
git push -u origin main
|
||
```
|
||
|
||
###### Pushing to Gitlab
|
||
7. Next, I added the (SSH) remote of the gitlab server to upload to as well.
|
||
```
|
||
git remote add NameofRemote git@gitblah.com:user/repo.git
|
||
```
|
||
|
||
8. I then created another branch for the upload sync to take place.
|
||
```
|
||
git branch AnotherBranch
|
||
git checkout AnotherBranch #switch into it
|
||
```
|
||
|
||
9.
|
||
|
||
10. Then follow the instructions [outlined on this article](https://techstreams.medium.com/git-google-drive-simple-git-host-3a84db4fc1fd). Basically you then create a git clone of it in your new directory by `git clone --bare . PATH_TO_GOOGLE_DRIVE_SYNC_FOLDER/ANY_SUBFOLDER_PATH/PROJECT_NAME.git`
|
||
11. Then do a git remote by `git remote add REMOTE_NAME PATH_TO_GOOGLE_DRIVE_SYNC_FOLDER/ANY_SUBFOLDER_PATH/PROJECT_NAME.git`
|
||
12. Add files to stage for commit and commit a message.
|
||
```
|
||
git add .
|
||
git commit -m "an update message here"
|
||
```
|
||
|
||
13. Lastly, push the changes onto your quantum programming repo.
|
||
```
|
||
git push -u origin main
|
||
```
|
||
Voila! You should see the changes now [over here](https://gitlab.com/shwetha729/quantum-programming). Or wherever you decided to set up host the git remote server on.
|
||
|
||
|
||
|
||
|
||
|
||
|
||
##### Resources & Referemces:
|
||
---
|
||
|
||
- Follow the instructions [here](https://medium.com/analytics-vidhya/how-i-put-my-mind-under-version-control-24caea37b8a5) and document as you go:
|
||
- on [Cron]( https://osxdaily.com/2020/04/27/fix-cron-permissions-macos-full-disk-access/):
|
||
- *UPDATE*: crontab is deprecated for MacOS so the sync option given will not work.
|
||
- the best next alternative is then to use [launchd](obsidian://open?vault=enter&file=Coding%20Tips%20(Classical)%2FTerminal%20Tips%2FShells%2FComputers%20(operating%20system)%2FApple%20Macbook%2FLaunchd) or keyboard scheduler as described through this youtube video.
|
||
- Refer back to [plists](obsidian://open?vault=Coding%20Tips&file=Computers%2FMac%20OS%20X%2FBBEdit%2Fplist) for a better understanding of implementation and usage.
|
||
- This [video that really helped me](https://www.youtube.com/watch?v=bVB24I7B0JQ) figure out Linode from the LearnLinux Youtube channel.
|
||
- apparently the extension [Obsidian-Git](https://github.com/denolehov/obsidian-git/wiki/Installation#existing-repo) already does this in an easier way now
|
||
- So this is how you [push it to your gitlab](https://about.gitlab.com/blog/2022/03/15/publishing-obsidian-notes-with-gitlab-pages/)
|
||
- Refer to the [Git page](obsidian://open?vault=Obsidian&file=Coding%20Tips%2FComputers%2FTerminal%20Tips%2FGit) on here for more shortcuts.
|
||
- This page exists now showing o[ne way to setup gitea with Obsidian](https://webuxlab.com/en/projects/gitea-obsidian)
|
||
|
||
|
||
|
||
|