Skip to main content
The GitClient class provides a simple, Pythonic interface for performing Git operations within your Mage pipeline blocks. This is useful for automating version control workflows, such as:
  • Automatically committing and pushing pipeline changes after successful runs
  • Syncing code from remote repositories before execution
  • Creating automated deployment pipelines

Prerequisites

Before using GitClient, ensure you have configured Git settings in Mage through one of these methods:
  1. Git Sync Settings (legacy): Configure via Settings → Workspace → Sync data, or see the Git Sync guide
  2. Deployment Settings (Mage Pro): Configure via the Deploy app at /apps/deploy

Quick Start

Basic Usage

Using Deployment Settings (Mage Pro)

Using GitClient in Pipeline Blocks

Data Exporter Block Example

The most common use case is pushing changes at the end of a pipeline:

Data Loader Block Example

Pull latest changes before processing:

GitClient Methods

Core Operations

Stage files for commit.Parameters:
  • files (str | List[str]): File path(s) to stage. Use '.' for all files.
  • flags (List[str], optional): Git add flags (e.g., ['-A'] to include deletions).
Returns: GitClient (for method chaining)
Commit staged changes.Parameters:
  • message (str): The commit message.
  • files (List[str], optional): Files to add before committing.
Returns: str - The commit hash
Push commits to the remote repository.Parameters:
  • remote_name (str, optional): Remote name. Defaults to configured remote.
  • branch_name (str, optional): Local branch name. Defaults to current branch.
  • remote_branch_name (str, optional): Remote branch name if different from local.
Returns: GitOperationResult
Pull changes from the remote repository.Parameters:
  • remote_name (str, optional): Remote name. Defaults to configured remote.
  • branch_name (str, optional): Branch name. Defaults to current branch.
Returns: GitOperationResult
Fetch changes from the remote without merging.Returns: GitOperationResult
Clone the remote repository (replaces local content).Parameters:
  • sync_submodules (bool): Whether to also sync git submodules.
Returns: GitOperationResult

Convenience Methods

Add, commit, and push changes in one operation. This is the recommended method for most use cases.Parameters:
  • message (str): The commit message.
  • files (str | List[str]): File path(s) to stage. Default: '.'
  • add_flags (List[str], optional): Git add flags (e.g., ['-A']).
  • remote_name (str, optional): Remote name.
  • branch_name (str, optional): Local branch name.
  • remote_branch_name (str, optional): Remote branch name if different.
Returns: GitOperationResult
Sync local repository with remote (fetch + reset —hard). Discards local changes.Parameters:
  • branch (str, optional): Branch to sync. Defaults to current branch.
Returns: GitOperationResult

Branch Operations

Switch to a different branch.
Merge another branch into the current branch.
Delete a local branch.

Status Properties

Get comprehensive git status information.
Get the current branch name.
Check if there are uncommitted changes.
Get list of modified files.
Get list of local branches.
Get list of configured remotes.
Get number of commits not yet pushed to remote.

GitOperationResult

All Git operations return a GitOperationResult object with the following attributes:

Method Chaining

GitClient supports method chaining for fluent API usage:

Advanced Examples

Conditional Push Based on Changes

Push to Multiple Branches

Pre-run Sync

Using the Built-in Git Push Template

Mage provides a built-in data exporter template for Git push operations. To add it to your pipeline:
  1. Click Blocks in the top navigation
  2. Select Exporter
  3. Navigate to Version controlGit push
Git push template navigation
The template provides a ready-to-use block with configurable options:

Setting Up Automated Git Push Jobs

You can automate Git push operations by creating a pipeline with a Git push block and configuring a trigger to run it on a schedule or in response to events.

Step 1: Create a Git Push Pipeline

  1. Create a new pipeline (e.g., auto_git_push)
  2. Add a Data Exporter block using the Git push template:
    • Click BlocksExporterVersion controlGit push
  3. Configure the block with your desired settings:

Step 2: Create a Trigger

Navigate to your pipeline’s Triggers tab and create a new trigger:
For periodic backups (e.g., daily at midnight):
  1. Click + New trigger
  2. Select Schedule as the trigger type
  3. Configure the schedule:
    • Name: daily_git_backup
    • Frequency: Daily (or use Custom with cron: 0 0 * * *)
    • Start date: Set your desired start date
  4. Click Save changes
Example cron patterns:
For more details on trigger configuration, see the Pipeline Triggers documentation.

Troubleshooting

If you encounter authentication errors:
  1. Verify your Git settings are configured correctly
  2. For SSH: Ensure your SSH keys are properly set up
  3. For HTTPS: Verify your access token has the correct permissions
  4. Check that the remote URL is accessible from your Mage instance
If you see “Permission denied” errors:
  1. Check that your user has write access to the repository
  2. Verify the branch is not protected (or you have permission to push to protected branches)
  3. Ensure your OAuth token hasn’t expired
If add_commit_push reports no changes:
  1. Check git.status to see the current state
  2. Verify files are not in .gitignore
  3. Use git.modified_files to see what files Git detects as changed