Skip to main content

Automating Git Commits with n8n

Introduction
#

Automatically committing file changes to Git is useful for many scenarios: backing up configuration files, versioning generated documentation, or tracking changes to data files. This tutorial shows you how to build an n8n workflow that watches a folder and automatically commits changes.

Download the Workflow
Get the ready-to-use workflow from our n8n Workflow Gallery.

Prerequisites
#

  • An n8n instance (self-hosted recommended for file system access)
  • Git installed on the server
  • SSH keys or credentials configured for Git push
  • A cloned repository on the server

Workflow Overview
#

1
File Change Detected → Git Add → Git Commit → Git Push

The workflow monitors a directory and triggers whenever files change, then stages, commits, and pushes the changes automatically.

Step-by-Step Setup
#

Step 1: Configure the File Trigger
#

  1. Add a Local File Trigger node
  2. Set the watch path to your target directory (e.g., /data/configs/)
  3. Enable recursive watching if you need subdirectories
  4. Select the events to watch: change, add, unlink
1
2
3
4
5
{
  "path": "/data/configs/",
  "events": ["change", "add", "unlink"],
  "recursive": true
}

Step 2: Stage Changes with Git Add
#

Add an Execute Command node:

1
cd /path/to/repo && git add -A

This stages all changes including new files, modifications, and deletions.

Step 3: Create the Commit
#

Add another Execute Command node:

1
cd /path/to/repo && git commit -m "Auto-commit: $(date '+%Y-%m-%d %H:%M:%S')"

The commit message includes a timestamp for easy tracking.

Step 4: Push to Remote
#

Add a final Execute Command node:

1
cd /path/to/repo && git push origin main

Enhanced Workflow Options
#

Add File Details to Commit Message
#

Include which file changed in the commit message:

1
cd /path/to/repo && git commit -m "Auto-commit: {{ $json.path }} changed at $(date '+%Y-%m-%d %H:%M:%S')"

Debounce Rapid Changes
#

If files change frequently, add a Wait node to batch changes:

1
File Trigger → Wait (30 seconds) → Git Add → Git Commit → Git Push

Add Error Handling
#

Wrap commands in error handling to catch failures:

1
cd /path/to/repo && git add -A && git commit -m "Auto-commit" || echo "Nothing to commit"

Notify on Success
#

Add a Slack notification after successful push:

1
Git Push → Slack: "Changes pushed to repo"

Testing the Workflow
#

  1. Activate the workflow in n8n
  2. Create or modify a file in the watched directory
  3. Check the n8n execution log
  4. Verify the commit appears in your Git history:
1
git log --oneline -5

Troubleshooting
#

Permission Denied on Push
#

  • Ensure SSH keys are configured for the n8n user
  • Or use HTTPS with credentials stored in Git credential helper

Nothing to Commit
#

  • The file might not be in the repository’s tracked path
  • Check .gitignore isn’t excluding the files

Workflow Triggers Too Often
#

  • Add debouncing with a Wait node
  • Or filter specific file patterns in the trigger

Security Considerations
#

  1. Limit watched directories - Don’t watch sensitive paths
  2. Use deploy keys - Create repo-specific SSH keys with limited permissions
  3. Review before production - Test thoroughly in a non-production repo first

Conclusion
#

Auto-committing file changes with n8n provides a simple backup and versioning solution for configurations, documentation, or any files that change regularly. Download the complete workflow from our n8n Workflow Gallery and adapt it to your needs.