This repository will help you to utilize fswatch
and launchd
in order to get your ZSH run configuration (.zshrc) synced with the git remote repository automatically.
If you want to deeply understand how it works, you should read this article: Medium - Organize and Auto Back Up Your ZSH Run Configuration
Follow these steps to automatically sync your .zshrc
with the remote repository.
-
Install required tools
fswatch: For detecting file changes, install it using Homebrew
brew install fswatch
-
Create git repository
mkdir ~/code && mkdir ~/code/zsh-backup && cd ~/code/zsh-backup git init gh repo create
Notes: If you haven't installed
github command-line
tool yet, please use:brew install gh
Remember to set the repo's visibility to Private.
-
Copy necessary files
- You should clone this repository and copy 3 files into
zsh-backup
folder: - Make scripts executable:
chmod +x commit_changes.sh && chmod +x sync_zshrc.sh
- Modify fswatch absolute path:
- Use
which fswatch
to get the absolute path offswatch
and replace/opt/homebrew/bin/fswatch
insync_zshrc.sh
with the result.
- Use
- You should clone this repository and copy 3 files into
-
Verify the scripts
- Navigate the
zsh-backup
directorycd ~/code/zsh-backup
- Run
sync_zshrc.sh
./sync_zshrc.sh
- Change the content of
~/.zshrc
or~/.zsh/
and check whether new changes are committed and pushed to the remote repository.
- Navigate the
-
Modify launchd property list file (com.[username].auto-sync-zshrc.plist)
-
Replace
imbaggaarm
with the username in your local machine. -
Replace
/Users/eastagile/
to the home directory of your logged in user.Notes: For StandardOutPath and StandardErrorPath in the .plist, you can use whatever you want, but please remember to make sure that the paths are existed (create if they are not) and your user has enough permissions to modify the files.
-
-
Make sync_zshrc run as a background service
- Copy
com.<username>.auto-sync-zshrc.plist
to~/Library/LaunchAgents/
:cp com.<username>.auto-sync-zshrc.plist ~/Library/LaunchAgents/
- Navigate to
~/Library/LaunchAgents/
and start the service:launchctl load com.<username>.auto-sync-zshrc.plist
- If you get
Load failed: 5: Input/output error
, please check if the .plist file is in the correct format and you are in the right directory.
- If you get
- You can check files at
StandardErrorPath
andStandardOutPath
to debug if there is anything wrong. In that case, I suggest you use LaunchControl for easier debugging. You can install it using Homebrew Cask:brew install --cask launchcontrol
- If you open the LaunchControl application and see all fields are green in
com.<username>.auto-sync-zshrc.plist
then congratulations, now all of your ZSH run configurations are synced automatically with the remote repository. Example: Notes: Remember to selectUserAgents
in the top left button of the LaunchControl application.
- Copy
-
Verify that our scripts are working properly
- Change
~/.zshrc
or~/.zsh/
and verify that our changes are committed and pushed automatically to the remote repository.
- Change
- You should store your private tokens, secrets in other files, such as
secrets.sh
, and load them by addingsource secrets.sh
into.zshrc
file. Even though our repository is private, but you might accidentally publish it or in case Github is hacked, your credentials won't be leaked.