|
| 1 | +--- |
| 2 | +title: Rules |
| 3 | +description: Automate change assignment with rules that match file patterns and automatically route changes to the right branch. |
| 4 | +--- |
| 5 | + |
| 6 | +import ImageSection from "@/components/ImageSection"; |
| 7 | + |
| 8 | +Rules are a powerful automation feature in GitButler that automatically assign file changes to specific branches based on conditions you define. Instead of manually dragging changes between lanes, you can set up rules that automatically route changes where they belong. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +When you're working on multiple branches simultaneously, you often know in advance which types of changes should go to which branch. For example, documentation updates might always go to a `docs` branch, while UI changes go to a `feature/ui-redesign` branch. |
| 13 | + |
| 14 | +Rules eliminate the manual work of assigning changes by automatically evaluating your uncommitted changes and routing them to the appropriate branch based on filters you define. |
| 15 | + |
| 16 | +## How Rules Work |
| 17 | + |
| 18 | +Rules are evaluated whenever files change in your working directory (the `fileSytemChange` trigger). Each rule consists of: |
| 19 | + |
| 20 | +1. **Filters**: Conditions that determine which changes the rule applies to |
| 21 | +2. **Action**: Assigns matching changes to a specific branch |
| 22 | + |
| 23 | +Multiple rules can exist, and they are evaluated in order. Within a single rule, multiple filters are combined with AND logic - all conditions must match for the rule to apply. |
| 24 | + |
| 25 | +## Creating a Rule |
| 26 | + |
| 27 | +To create a rule: |
| 28 | + |
| 29 | +1. Open the **Rules** drawer at the bottom of the GitButler interface |
| 30 | +2. Click the **+** button to add a new rule |
| 31 | +3. Select the target branch where matching changes should be assigned: |
| 32 | + - Specify a branch by name |
| 33 | + - **Leftmost lane**: The leftmost branch in your workspace |
| 34 | + - **Rightmost lane**: The rightmost branch in your workspace |
| 35 | +4. (Optional) Add filters to specify which changes should match |
| 36 | +5. Click **Save rule** |
| 37 | + |
| 38 | +If you don't add any filters, the rule will match all changes. |
| 39 | + |
| 40 | +<video |
| 41 | + src="https://gitbutler-docs-images-public.s3.us-east-1.amazonaws.com/rule-left-rightmost.mp4" |
| 42 | + controls |
| 43 | + muted |
| 44 | + autoPlay |
| 45 | + loop |
| 46 | + className="rounded-lg border border-gray-200 dark:border-gray-800" |
| 47 | +> |
| 48 | + Your browser does not support the video tag. |
| 49 | +</video> |
| 50 | +<p className="text-center text-sm text-gray-600 dark:text-gray-400 mt-2"> |
| 51 | + Creating a rule that assigns changes to the leftmost lane |
| 52 | +</p> |
| 53 | + |
| 54 | +## Filter Types |
| 55 | + |
| 56 | +Rules support several types of filters to match changes: |
| 57 | + |
| 58 | +### Path Matches Regex |
| 59 | + |
| 60 | +Matches file paths using a regular expression pattern. |
| 61 | + |
| 62 | +**Example use cases:** |
| 63 | + |
| 64 | +- Match all TypeScript files: `.*\.ts$` |
| 65 | +- Match files in a specific directory: `^src/components/.*` |
| 66 | +- Match documentation files: `.*\.(md|mdx)$` |
| 67 | + |
| 68 | +### Content Matches Regex |
| 69 | + |
| 70 | +Matches the content of changed lines using a regular expression pattern. This filter only looks at added lines (lines that start with `+` in the diff). |
| 71 | + |
| 72 | +**Example use cases:** |
| 73 | + |
| 74 | +- Match changes containing TODOs: `TODO` |
| 75 | +- Match changes with specific function calls: `console\.log` |
| 76 | +- Match changes with certain patterns: `@deprecated` |
| 77 | + |
| 78 | +<video |
| 79 | + src="https://gitbutler-docs-images-public.s3.us-east-1.amazonaws.com/rule-text-match.mp4" |
| 80 | + controls |
| 81 | + muted |
| 82 | + autoPlay |
| 83 | + loop |
| 84 | + className="rounded-lg border border-gray-200 dark:border-gray-800" |
| 85 | +> |
| 86 | + Your browser does not support the video tag. |
| 87 | +</video> |
| 88 | +<p className="text-center text-sm text-gray-600 dark:text-gray-400 mt-2"> |
| 89 | + Creating a rule that assigns changes containing "fix" to a specific branch |
| 90 | +</p> |
| 91 | + |
| 92 | +### Claude Code Session ID |
| 93 | + |
| 94 | +Matches changes that originated from a specific Claude Code session. This filter is automatically used when GitButler's AI features create branches and rules for you. |
| 95 | + |
| 96 | +**Note**: Rules with Claude Code Session ID filters have lower priority than manually created rules. If a change matches both an AI-generated rule and a manual rule, the manual rule takes precedence. |
| 97 | + |
| 98 | +<video |
| 99 | + src="https://gitbutler-docs-images-public.s3.us-east-1.amazonaws.com/rule-ai.mp4" |
| 100 | + controls |
| 101 | + muted |
| 102 | + autoPlay |
| 103 | + loop |
| 104 | + className="rounded-lg border border-gray-200 dark:border-gray-800" |
| 105 | +> |
| 106 | + Your browser does not support the video tag. |
| 107 | +</video> |
| 108 | +<p className="text-center text-sm text-gray-600 dark:text-gray-400 mt-2"> |
| 109 | + AI-generated rules based on Claude Code sessions |
| 110 | +</p> |
| 111 | + |
| 112 | +## Managing Rules |
| 113 | + |
| 114 | +### Editing Rules |
| 115 | + |
| 116 | +To edit an existing rule: |
| 117 | + |
| 118 | +1. Double-click the rule or click the elepsis menu (...) and select "Edit rule" |
| 119 | +2. Modify the branch assignment or filters |
| 120 | +3. Click **Save rule** |
| 121 | + |
| 122 | +### Deleting Rules |
| 123 | + |
| 124 | +To delete a rule: |
| 125 | + |
| 126 | +1. Click the elepsis menu (...) on the rule |
| 127 | +2. Select "Delete rule" |
| 128 | +3. Confirm the deletion |
| 129 | + |
| 130 | +**Note**: AI-generated rules and implicit rules cannot be edited. |
| 131 | + |
| 132 | +## Understanding Rule Evaluation |
| 133 | + |
| 134 | +### Order Matters |
| 135 | + |
| 136 | +Rules are evaluated in the order they appear in the Rules drawer (most recent first). The first matching rule determines where a change is assigned. |
| 137 | + |
| 138 | +### AND Logic Within Rules |
| 139 | + |
| 140 | +When a rule has multiple filters, **all filters must match** for the rule to apply. For example, a rule with both "Path Matches Regex: `.*\.ts$`" and "Content Matches Regex: `TODO`" will only match TypeScript files that contain the text "TODO" in their changes. |
| 141 | + |
| 142 | +### OR Logic Across Rules |
| 143 | + |
| 144 | +If you want to match changes that meet any of several conditions (OR logic), create separate rules for each condition. |
| 145 | + |
| 146 | +### Interaction with Hunk Dependencies |
| 147 | + |
| 148 | +Rules respect hunk dependencies (locks). If a change depends on a commit in a specific branch, it cannot be automatically reassigned by rules, even if it matches a rule's filters. |
| 149 | + |
| 150 | +## Best Practices |
| 151 | + |
| 152 | +1. **Start simple**: Begin with one or two basic path-matching rules before adding complex filters |
| 153 | +2. **Order your rules**: Place more specific rules before general catch-all rules |
| 154 | +3. **Test your regex**: Make sure your regular expressions match what you intend - it's easy to be too broad or too narrow |
| 155 | +4. **Use catch-all rules carefully**: A rule with no filters will match everything, which can interfere with other rules |
| 156 | +5. **Consider your workflow**: Rules work best when you have predictable patterns in how your work is organized |
| 157 | +6. **Leverage leftmost/rightmost**: Using position-based targeting lets you reorganize lanes without updating rules |
| 158 | + |
| 159 | +## Limitations |
| 160 | + |
| 161 | +- Rules can only assign changes to branches that exist in your workspace (applied branches) |
| 162 | +- Implicit (AI-determined) rules cannot be edited through the UI |
| 163 | +- Rules currently only support the `assign` action for filesystem changes |
| 164 | + |
| 165 | +## Related Features |
| 166 | + |
| 167 | +- [Virtual Branches](/docs/features/branch-management/virtual-branches): Understanding the branch system that rules work with |
| 168 | +- [Branch Lanes](/docs/features/branch-management/branch-lanes): How lanes are organized and how rules interact with lane positioning |
| 169 | +- [AI Assistance](/docs/features/branch-management/ai-assistance): How AI can create and manage rules automatically |
0 commit comments