A template with magic aliases, hooks, services for Git repository auto-synchronization between different devices.
This repository provides a sync
Git command that automatically commits and sends changes to origin. This can be useful if you maintain a website using Hugo or write any notes in a program such as Obsidian. In addition, the repository provides services and a job scheduler for some platforms (Windows, Linux, Termux) which runs every half hour.
-
Copy the
.githooks
directory into your Git repository folder.cp -r .githooks <your Git repo>
-
Go to your Git repository:
cd <your Git repo>
-
Go to the
install
directory:cd .githooks/install
-
Run the installation script:
-
Linux (Bash):
bash -e ./install-default.sh
or
make install
-
Windows (PowerShell):
PowerShell.exe -ExecutionPolicy Bypass -File .\install-default.ps1
-
-
Run instead
git pull
/git commit
/git push
:git sync
Go to the install
directory:
cd .githooks/install
Install using Make:
$ make install
Install using shell:
$ bash -e ./install-default.sh
Install using Make:
$ make systemd
Install using shell:
$ bash -e ./install-systemd.sh
Check the output of the systemd
-service:
$ journalctl -e --user-unit <your repo name>-sync.service
Install using Make:
$ make cron
Install using shell:
$ bash -e ./install-cron.sh
Check the log output in ~/.<your repo name>-sync.log
.
Install using Make:
$ make install
Install using shell:
$ bash -e ./install-default.sh
Install using Make:
$ make cron
Install using shell:
$ bash -e ./install-cron.sh
Check the log output in ~/.<your repo name>-sync.log
.
There were some problems with Termux, which was how to get it to work all the time. You can use Termux-services to make synchronization work properly. This application does not have a full-fledged initialization system like GNU/Linux, but this works and allows you to run scripts on the machine.
To run scripts when the app autoruns, you must install the Termux:Boot addon and setup a simple boot-config. Use the code below.
-
Install
termux-services
andcronie
:$ pkg install -y cronie termux-services
-
Enable
crond
service:$ sv-enable crond
-
Create an autorun script for
crond
:$ cat > ~/.termux/boot/start-crond << EOF #!/data/data/com.termux/files/usr/bin/sh termux-wake-lock sv-enable crond EOF
-
Restart Termux.
P.S. Sometimes it doesn't work with some Android devices. In this case, use the Termux-wake-lock function or, as an alternative to Termux:Boot, Termux:Tasker.
pkg install -y cronie termux-services
sv-enable crond
cat > ~/.termux/boot/start-crond << EOF
#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
sv-enable crond
EOF
Restart Termux.
Install using PowerShell:
PS> PowerShell.exe -ExecutionPolicy Bypass -File .\install-default.ps1
Run this script to create a job for Task Scheduler:
PS> PowerShell.exe -ExecutionPolicy Bypass -File .\install-task.ps1
The git sync
command provides the following scenario:
- Stash Stage.
- Commit Stage.
- Push Stage.
It uses Git hooks, so you can extend your hooks, for example, by adding your scripts to the run_health_check
section of the pre-commit
file.
Get the changes, hide them in stash, merge them, and bring them back.
git fetch --all
git stash
git merge @{u}
git stash pop
Add changes, commit, and clear stash.
git add -A
if [[ -z "$(git status --porcelain)" ]]
then
git commit -m "UPDATE from <OS name> by <time now>"
fi
git stash clear
Push using post-commit
hook:
git push --all origin