Skip to content
/ git-sync Public template

A template with magic aliases, hooks, services for Git repository auto-synchronization between different devices.

License

Notifications You must be signed in to change notification settings

Alimektor/git-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

git-sync

A template with magic aliases, hooks, services for Git repository auto-synchronization between different devices.

Table of Contents

What Is It?

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.

Quickstart

  1. Copy the .githooks directory into your Git repository folder.

    cp -r .githooks <your Git repo>
  2. Go to your Git repository:

    cd <your Git repo>
  3. Go to the install directory:

    cd .githooks/install
  4. Run the installation script:

    • Linux (Bash):

      bash -e ./install-default.sh

      or

      make install
    • Windows (PowerShell):

      PowerShell.exe -ExecutionPolicy Bypass -File .\install-default.ps1
  5. Run instead git pull / git commit / git push:

    git sync

Installation

Go to the install directory:

cd .githooks/install

Linux

Manual Usage

Install using Make:

$ make install

Install using shell:

$ bash -e ./install-default.sh

SystemD

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

Cron

Install using Make:

$ make cron

Install using shell:

$ bash -e ./install-cron.sh

Check the log output in ~/.<your repo name>-sync.log.

Termux

Manual Usage

Install using Make:

$ make install

Install using shell:

$ bash -e ./install-default.sh

Cron

Install using Make:

$ make cron

Install using shell:

$ bash -e ./install-cron.sh

Check the log output in ~/.<your repo name>-sync.log.

Additional Information

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.

  1. Install termux-services and cronie:

    $ pkg install -y cronie termux-services
  2. Enable crond service:

    $ sv-enable crond
  3. 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
  4. 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.

Shortcut
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.

Windows

Manual Usage

Install using PowerShell:

PS> PowerShell.exe -ExecutionPolicy Bypass -File .\install-default.ps1

Task Scheduler

Run this script to create a job for Task Scheduler:

PS> PowerShell.exe -ExecutionPolicy Bypass -File .\install-task.ps1

How It Works

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.

Stash Stage

Get the changes, hide them in stash, merge them, and bring them back.

git fetch --all
git stash
git merge @{u}
git stash pop

Commit Stage

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 Stage

Push using post-commit hook:

git push --all origin

License

License: MIT

About

A template with magic aliases, hooks, services for Git repository auto-synchronization between different devices.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published