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 --repo=repository_url --branch=branch_name --dest=destination_directory [options]

PARAMETERS

--repo=repository_url
    Specifies the URL of the remote Git repository to be mirrored.

--branch=branch_name
    Specifies the branch name to be synchronized.

--dest=destination_directory
    Specifies the local directory where the repository will be mirrored.

--rev=revision
    Sync to a specific revision. Can be a commit, tag or branch. Default is to sync to the head of the branch specified with --branch. Mutually exclusive with --branch.

--depth=depth
    Specifies the depth of the clone to be performed.

--one-time
    Perform only one sync operation and then exit. Useful for initialization purposes.

--wait=seconds
    Specifies the interval in seconds to wait between synchronization attempts.

--max-sync-failures=number
    Specifies the maximum number of failed sync attempts before exiting.

--ssh-key=path_to_ssh_key
    Specifies the path to the SSH private key to use for authentication (if required).

--username=username
    Specifies the username for authentication with the Git repository.

--password=password
    Specifies the password for authentication with the Git repository.

--retry=number
    The number of retries to sync before giving up.

DESCRIPTION

The git-sync script is designed for efficiently mirroring a Git repository from a remote source to a local destination. It is commonly employed in environments where automated repository updates are required, such as deploying application code to servers or keeping development environments synchronized.
It optimizes the synchronization process by leveraging Git's incremental update capabilities, fetching only the changes that have occurred since the last synchronization. This approach significantly reduces network traffic and synchronization time compared to a full clone operation.
It is frequently used in conjunction with tools like Kubernetes for code deployment or automated configuration management.
Main use case is for mirroring a repository to a different location, which is useful in cloud computing environments, for example.

CAVEATS

Requires Git to be installed and configured on the system. Proper permissions are required for accessing the remote repository and writing to the destination directory. Care should be taken when using with passwords directly in the command line, it is better to use SSH keys for authentication.

AUTHENTICATION

git-sync supports various authentication methods, including SSH keys, username/password credentials, and Git credential helpers. The appropriate method should be chosen based on the security requirements and configuration of the Git repository.

ERROR HANDLING

The --max-sync-failures flag can be used to specify the maximum number of failed sync attempts before the script exits. This can prevent infinite retries in case of persistent network issues or authentication problems. Using the --retry flag can also improve resilience.

HISTORY

git-sync emerged as a solution for automated repository mirroring, particularly in cloud-native environments. It addresses the need for continuous code deployment and synchronization, streamlining workflows in containerized applications and infrastructure-as-code deployments.

SEE ALSO

git(1), rsync(1)

Copied to clipboard