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
from mage_ai.deployments.git_client import GitClient# Initialize the client (uses legacy git sync settings by default)git = GitClient()# Add, commit, and push all changes in one operationresult = git.add_commit_push('Auto-commit from Mage pipeline')if result.success: print(f"✓ Pushed to {result.remote}/{result.branch}") print(f" Commit: {result.commit_hash}")else: print(f"✗ Failed: {result.error}")
from mage_ai.deployments.git_client import GitClient# Use deployment settings instead of legacy git syncgit = GitClient(use_deployment_settings=True)result = git.add_commit_push('Deploy changes via Mage Pro')
The most common use case is pushing changes at the end of a pipeline:
from mage_ai.deployments.git_client import GitClientif 'data_exporter' not in globals(): from mage_ai.data_preparation.decorators import data_exporter@data_exporterdef export_data(*args, **kwargs): """ Push pipeline changes to Git after processing data. """ # Initialize GitClient git = GitClient() # Push all changes result = git.add_commit_push( message='Pipeline run completed - auto commit', files='.', # Stage all files add_flags=['-A'], # Include deletions ) if result.success: print(f"✓ Successfully pushed to {result.remote}/{result.branch}") else: print(f"✗ Push failed: {result.error}") return { 'git_result': result.to_dict(), }
from mage_ai.deployments.git_client import GitClientif 'data_loader' not in globals(): from mage_ai.data_preparation.decorators import data_loader@data_loaderdef load_data(*args, **kwargs): """ Pull latest changes from remote before loading data. """ git = GitClient() # Pull latest changes result = git.pull() if result.success: print(f"✓ Pulled latest from {result.remote}/{result.branch}") else: print(f"⚠ Pull failed: {result.error}") # Your data loading logic here return {'status': 'loaded'}
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
# Push to default remote/branchresult = git.push()# Push to specific remote and branchresult = git.push(remote_name='origin', branch_name='main')# Push local 'develop' to remote 'staging'result = git.push( remote_name='origin', branch_name='develop', remote_branch_name='staging',)
pull(remote_name, branch_name)
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
result = git.pull()result = git.pull(remote_name='origin', branch_name='main')
fetch()
Fetch changes from the remote without merging.Returns:GitOperationResult
result = git.fetch()
clone(sync_submodules)
Clone the remote repository (replaces local content).Parameters:
sync_submodules (bool): Whether to also sync git submodules.
remote_branch_name (str, optional): Remote branch name if different.
Returns:GitOperationResult
# Simple push of all changesresult = git.add_commit_push('Update from pipeline')# Push specific filesresult = git.add_commit_push( message='Update config', files=['pipelines/', 'data_loaders/'],)# Push including deletionsresult = git.add_commit_push( message='Clean up', files='.', add_flags=['-A'],)# Push to a different remote branchresult = git.add_commit_push( message='Deploy to staging', branch_name='main', remote_branch_name='staging',)
sync(branch)
Sync local repository with remote (fetch + reset —hard). Discards local changes.Parameters:
branch (str, optional): Branch to sync. Defaults to current branch.
from mage_ai.deployments.git_client import GitClient@data_exporterdef export_data(data, *args, **kwargs): git = GitClient() # Only push if there are actual changes if git.is_dirty: print(f"Found {len(git.modified_files)} modified files") result = git.add_commit_push('Auto-commit changes') return {'pushed': result.success, 'commit': result.commit_hash} else: print("No changes to commit") return {'pushed': False, 'message': 'No changes'}
from mage_ai.deployments.git_client import GitClient@data_loaderdef load_with_sync(*args, **kwargs): git = GitClient() # Fetch latest and check for updates git.fetch() if git.unpushed_commits_count < 0: # We're behind print("Local is behind remote, syncing...") git.sync() # Now load your data return load_data_from_synced_repo()
Mage provides a built-in data exporter template for Git push operations. To add it to your pipeline:
Click Blocks in the top navigation
Select Exporter
Navigate to Version control → Git push
The template provides a ready-to-use block with configurable options:
# Parameters available in the template:commit_message: str # Commit messagefiles: str | List[str] # Files to stage (default: '.')include_deletions: bool # Include deleted files (default: True)remote_name: str # Remote name (optional)branch_name: str # Branch name (optional)use_deployment_settings: bool # Use Mage Pro settings (default: False)
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.