LinuxCommandLibrary

git-sync

Synchronize a local directory from a Git repository

TLDR

Sync the current local branch with its remote branch

$ git sync
copy

Sync the current local branch with the remote main branch
$ git sync origin main
copy

Sync without cleaning untracked files
$ git sync [[-s|--soft]] [remote_name] [branch_name]
copy

SYNOPSIS

git-sync [options] <dest_dir>

PARAMETERS

--repo=<URL>
    Git repository URL (required unless default).

--branch=<name>
    Branch name to track (default: HEAD).

--rev=<ref>
    Specific revision/tag (exclusive with --branch).

--depth=<N>
    Shallow clone depth (default: 1).

--resync-interval=<duration>
    Sync interval, e.g., 10s (default: 10s).

--max-resync-retries=<N>
    Max retries before quit (default: 0=infinite).

--submodules=<mode>
    none|shallow|recursive (default: none).

--cookiefile=<path>
    Netscape cookie file for HTTPS auth.

--one-time-only
    Sync once and exit.

--touch=<path>
    Touch file after successful sync.

--link-dest=<path>
    Rsync --link-dest path for efficiency.

--dest-lockfile=<path>
    Lockfile in dest (default: .git-sync-lock).

--verbose
    Increase logging verbosity.

--quiet
    Reduce logging.

-h, --help
    Show help.

--version
    Print version.

DESCRIPTION

git-sync is a utility for keeping a local directory synchronized with a specific branch, tag, or revision from a remote Git repository.

It clones the repository shallowly by default and periodically fetches updates, making it ideal for one-way synchronization in environments like Kubernetes deployments, CI/CD pipelines, or containers where full Git operations are undesirable.

Key benefits:
• Low resource usage with configurable shallow depth and resync intervals.
• Resilient to network failures with retry logic.
• Supports authentication via cookie jars (HTTPS) or SSH keys.
• Optional hard-linking for efficient updates via --link-dest.

By default, it runs indefinitely, syncing every 10 seconds, but can perform a one-time sync and exit. It creates a lockfile to prevent concurrent runs and touches an optional file on success for health checks.

Common use case: Mount a volume at dest, run git-sync, and consume updated files from apps. Requires Git 2.0+ installed.

CAVEATS

One-way sync only; changes in dest are overwritten.
Not for interactive Git use; requires writable dest.
No support for Git LFS natively.

KUBERNETES EXAMPLE

Deployment snippet:
command: ["/git-sync"]
args: ["--repo=https://github.com/example/repo", "--branch=main", "--depth=1", "--period=1m", "/dest"]
volumeMounts: [{name: "dest", mountPath: "/dest"}]

AUTHENTICATION

For private repos: Use --cookiefile for GitHub tokens or --ssh with keys.
Mount secrets as volumes.

HISTORY

Developed by Google Cloud Platform ~2017 for Kubernetes ConfigMaps/Secrets.
Open-sourced; actively maintained for container workflows.

SEE ALSO

git(1), rsync(1)

Copied to clipboard