Source Control (Git Sync)
Relevant source files
The following files were used as context for generating this wiki page:
- packages/cli/src/environments.ee/source-control/source-control.service.ee.ts
- packages/cli/src/environments.ee/source-control/source-control.controller.ee.ts
- packages/cli/src/environments.ee/source-control/source-control-git.service.ee.ts
- packages/cli/src/environments.ee/source-control/source-control.config.ts
- packages/frontend/editor-ui/src/constants.ts
The Source Control (Git Sync) feature is an enterprise capability that allows users to version control their n8n workflows and credentials by synchronizing them with a remote Git repository. This enables collaborative development, provides a complete history of changes, and facilitates backup and disaster recovery. The system is designed to manage the serialization of n8n entities into a file-based Git repository and deserialize them back into the n8n database.
The core functionality is handled by a set of services that manage Git operations, business logic for pushing and pulling data, and API endpoints for frontend interaction. The feature is triggered by the user through specific modals in the n8n UI, such as the push and pull modals.
Architecture
The Source Control feature is composed of three main backend components that work together to provide the Git synchronization functionality. The interaction starts from the API controller, which delegates business logic to the main service, which in turn uses a specialized Git service to interact with the repository.
This diagram illustrates the high-level architecture of the Source Control system.
Components
| Component | Description | File |
|---|---|---|
SourceControlController | Exposes REST API endpoints for the frontend to interact with the source control features (e.g., push, pull, status). | source-control.controller.ee.ts |
SourceControlService | Contains the core business logic. It orchestrates the process of fetching data from the database, preparing it for Git, and saving it back after a pull. | source-control.service.ee.ts |
SourceControlGitService | A low-level service that acts as a wrapper around the simple-git library. It is responsible for executing all Git commands like clone, pull, add, commit, and push. | source-control-git.service.ee.ts |
sourceControlConfig | Provides the configuration for the feature, loaded from environment variables. | source-control.config.ts |
Configuration
The Source Control feature is configured via environment variables. The configuration is loaded and validated using a dedicated config factory.
Sources: packages/cli/src/environments.ee/source-control/source-control.config.ts:9-32
| Variable | Description | Required | Default |
|---|---|---|---|
N8N_SOURCE_CONTROL_GIT_REPOSITORY_URL | The URL of the remote Git repository to sync with. | Yes | undefined |
N8N_SOURCE_CONTROL_GIT_USERNAME | The username for authenticating with the remote repository. | No | undefined |
N8N_SOURCE_CONTROL_GIT_PASSWORD | The password or personal access token for authentication. | No | undefined |
N8N_SOURCE_CONTROL_GIT_AUTHOR_NAME | The name of the author for Git commits. | Yes | undefined |
N8N_SOURCE_CONTROL_GIT_AUTHOR_EMAIL | The email of the author for Git commits. | Yes | undefined |
N8N_SOURCE_CONTROL_GIT_AUTO_CRON | A cron expression to enable automatic synchronization. | No | undefined |
API Endpoints
The SourceControlController defines the API endpoints used by the n8n frontend to manage Git operations. All endpoints are protected and require the sourceControl feature to be enabled.
Sources: packages/cli/src/environments.ee/source-control/source-control.controller.ee.ts:19-61
| Method | Endpoint | Description |
|---|---|---|
GET | /v1/source-control/status | Retrieves the current status of the local repository, including modified, new, and deleted files. |
POST | /v1/source-control/push | Pushes local changes (workflows and credentials) to the remote repository. Requires a commit message. |
POST | /v1/source-control/pull | Pulls the latest changes from the remote repository and applies them to the n8n instance. |
GET | /v1/source-control/history | Gets the commit history of the repository. |
GET | /v1/source-control/commit/:hash | Retrieves the details of a specific commit by its hash. |
Key Operations
The primary operations of this feature are pushing local changes to the remote repository and pulling remote changes to the local n8n instance.
Push Operation
The push operation serializes all workflows and credentials from the n8n database into JSON files, commits them to the local Git repository, and pushes them to the configured remote.
The following sequence diagram shows the flow of a push request.
Pull Operation
The pull operation fetches the latest changes from the remote repository. It then reads the JSON files representing workflows and credentials, deserializes them, and upserts them into the n8n database. This updates the local instance to match the state of the repository.
The sequence diagram below details the pull operation flow.
Frontend Integration
The Source Control feature is initiated from the n8n editor UI. The frontend contains constants that define keys for modals used to interact with the Git Sync functionality. These modals provide the user interface for entering commit messages and triggering push/pull actions.
SOURCE_CONTROL_PUSH_MODAL_KEY: Identifier for the modal used to push changes.SOURCE_CONTROL_PULL_MODAL_KEY: Identifier for the modal used to pull changes.
These constants link the UI components to the backend API calls.
Sources: packages/frontend/editor-ui/src/constants.ts:70-71
Summary
The Source Control (Git Sync) feature provides a robust mechanism for versioning n8n assets. By integrating directly with Git, it allows development teams to apply standard software engineering practices like code reviews, version history, and branching to their automation workflows. The architecture is cleanly separated into layers for handling API requests, business logic, and direct Git interactions, making it a maintainable and extensible enterprise feature.