Skip to content

Instantly share code, notes, and snippets.

@Disturbing
Created December 16, 2025 02:08
Show Gist options
  • Select an option

  • Save Disturbing/c19a560e52fbe107e4c9774eb1d9cf68 to your computer and use it in GitHub Desktop.

Select an option

Save Disturbing/c19a560e52fbe107e4c9774eb1d9cf68 to your computer and use it in GitHub Desktop.
Nullshot AI Merge Strategy
# Git Merge from Main - Safe Merge Protocol
You are performing a CRITICAL code merge operation. Code loss during merges is a severe issue that can destroy hours of work. Follow this protocol EXACTLY and use Chain-of-Thought reasoning at every step.
---
## 🚨 CRITICAL RULES - MEMORIZE THESE
### NEVER DO THESE (Violations cause code loss):
- ❌ NEVER accept one side wholesale without analysis
- ❌ NEVER use `git checkout --theirs <file>`
- ❌ NEVER use `git checkout --ours <file>`
- ❌ NEVER use `-X theirs` or `-X ours` merge strategies
- ❌ NEVER assume the shorter version is correct
- ❌ NEVER skip the pre-merge analysis phase
- ❌ NEVER resolve conflicts without showing what would be LOST
- ❌ NEVER proceed if you don't fully understand both sides of a conflict
### ALWAYS DO THESE (Required for safe merges):
- ✅ ALWAYS perform line count comparison BEFORE merging
- ✅ ALWAYS search for imports, hooks, state variables, and key features on BOTH sides
- ✅ ALWAYS show explicit report of what would be LOST with each resolution option
- ✅ ALWAYS merge BOTH sets of changes when they're not truly conflicting
- ✅ ALWAYS keep all imports, hooks, state from both sides unless truly incompatible
- ✅ ALWAYS verify final line count is ≥ max(ours, theirs)
- ✅ ALWAYS ask questions when uncertain
- ✅ ALWAYS use `GIT_EDITOR=false` to prevent interactive terminals
---
## PHASE 1: PRE-MERGE ANALYSIS (REQUIRED)
Before ANY merge operation, perform this complete analysis:
### Step 1.1: Fetch and Compare Branches
```bash
git fetch origin main
git log --oneline HEAD..origin/main # What's new in main
git log --oneline origin/main..HEAD # What's unique to our branch
```
### Step 1.2: Generate Diff Statistics
```bash
git diff --stat origin/main
git diff --numstat origin/main
```
### Step 1.3: Line Count Analysis (🚩 RED FLAG DETECTION)
For EACH file that will be affected:
```bash
# Get line counts for comparison
wc -l <file> # Current (ours)
git show origin/main:<file> | wc -l # Main's version (theirs)
```
**🚩 RED FLAG: If main has 50+ more lines than our version, this indicates main likely has NEW FEATURES that we must preserve.**
Report format:
```
FILE ANALYSIS: src/components/Example.tsx
├── Our version: 245 lines
├── Main's version: 312 lines
├── Difference: +67 lines in main
└── 🚩 RED FLAG: Main has significantly more code - likely contains new features!
```
### Step 1.4: Feature Detection Analysis
For each conflicting file, search for and catalog:
```bash
# Search for key patterns in BOTH versions
# Our version
grep -n "import\|export\|useState\|useEffect\|useCallback\|useMemo\|useRef\|const\s\+\w\+\s*=" <file>
# Main's version
git show origin/main:<file> | grep -n "import\|export\|useState\|useEffect\|useCallback\|useMemo\|useRef\|const\s\+\w\+\s*="
```
Create a feature inventory:
```
FEATURE INVENTORY: src/components/Example.tsx
IMPORTS:
├── Ours only: [list imports unique to our branch]
├── Main only: [list imports unique to main]
└── Both: [list shared imports]
HOOKS & STATE:
├── Ours only: [list hooks/state unique to our branch]
├── Main only: [list hooks/state unique to main]
└── Both: [list shared hooks/state]
FUNCTIONS:
├── Ours only: [list functions unique to our branch]
├── Main only: [list functions unique to main]
└── Both: [list shared functions]
KEY FEATURES:
├── Ours only: [describe features unique to our branch]
└── Main only: [describe features unique to main]
```
### Step 1.5: Pre-Merge Report (REQUIRED OUTPUT)
Before proceeding, output this report and WAIT for confirmation:
```
═══════════════════════════════════════════════════════════════
PRE-MERGE ANALYSIS REPORT
═══════════════════════════════════════════════════════════════
BRANCH COMPARISON:
├── Our branch: [branch name]
├── Base: origin/main
├── Commits in main we don't have: [count]
└── Commits in ours not in main: [count]
FILES TO BE MERGED: [count]
🚩 RED FLAGS DETECTED: [count]
[List each red flag with file name and reason]
FEATURES AT RISK OF BEING LOST:
[List every feature from main that could be lost if we're careless]
FEATURES WE MUST PRESERVE FROM OUR BRANCH:
[List every feature from our branch that must be kept]
═══════════════════════════════════════════════════════════════
```
**ASK USER: "I've completed the pre-merge analysis. Should I proceed with the merge? (yes/no)"**
---
## PHASE 2: ATTEMPT THE MERGE
Only after Phase 1 is complete and approved:
```bash
GIT_EDITOR=false git merge origin/main --no-commit
```
If conflicts occur, proceed to Phase 3. If no conflicts, proceed to Phase 4.
---
## PHASE 3: CONFLICT RESOLUTION PROTOCOL
### Step 3.1: List All Conflicts
```bash
git diff --name-only --diff-filter=U
```
### Step 3.2: For EACH Conflicting File
#### A) Show the Full Conflict Context
Display the conflict with surrounding context (at least 10 lines each side):
```
═══════════════════════════════════════════════════════════════
CONFLICT IN: src/components/Example.tsx
═══════════════════════════════════════════════════════════════
<<<<<<< HEAD (OUR CHANGES)
[Show our version with context]
=======
[Show main's version with context]
>>>>>>> origin/main (MAIN'S CHANGES)
```
#### B) Analyze What Each Resolution Would DELETE
**THIS IS CRITICAL - Always show deletion impact:**
```
RESOLUTION OPTIONS:
┌─────────────────────────────────────────────────────────────┐
│ OPTION A: Accept OURS only │
├─────────────────────────────────────────────────────────────┤
│ ⚠️ WOULD DELETE from main: │
│ - [import X from 'y'] │
│ - [useState hook for featureZ] │
│ - [handleNewFeature function] │
│ - [Lines 45-89 containing new feature logic] │
│ │
│ 🚫 NOT RECOMMENDED - Loses main's changes │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ OPTION B: Accept THEIRS only │
├─────────────────────────────────────────────────────────────┤
│ ⚠️ WOULD DELETE from ours: │
│ - [import A from 'b'] │
│ - [useEffect hook for our feature] │
│ - [handleOurFeature function] │
│ - [Lines 23-67 containing our feature logic] │
│ │
│ 🚫 NOT RECOMMENDED - Loses our changes │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ OPTION C: ✅ MERGE BOTH (RECOMMENDED) │
├─────────────────────────────────────────────────────────────┤
│ ✅ KEEPS from ours: │
│ - [list everything preserved] │
│ │
│ ✅ KEEPS from main: │
│ - [list everything preserved] │
│ │
│ 📝 REQUIRES: Manual integration of both changes │
│ │
│ ✅ RECOMMENDED - Preserves all work │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ OPTION D: Custom Resolution │
├─────────────────────────────────────────────────────────────┤
│ Explain what you want to keep/remove and I'll help │
└─────────────────────────────────────────────────────────────┘
```
#### C) ASK FOR CONFIRMATION
**REQUIRED: Never auto-resolve. Always ask:**
```
Which resolution would you like for this conflict?
- A: Accept ours only (⚠️ loses main's changes)
- B: Accept theirs only (⚠️ loses our changes)
- C: Merge both (✅ recommended - I'll integrate both)
- D: Custom (explain what you want)
Please choose A, B, C, or D:
```
#### D) If User Chooses C (Merge Both)
Perform intelligent merge:
1. **Start with the larger file as base** (usually has more features)
2. **Add all imports from both sides**
3. **Add all hooks/state from both sides**
4. **Add all functions from both sides**
5. **Integrate UI/logic changes carefully**
6. **Check for naming conflicts and resolve them**
7. **Ensure all features work together**
---
## PHASE 4: FINAL VERIFICATION (REQUIRED)
### Step 4.1: Line Count Verification
```bash
# For each merged file
wc -l <merged-file>
```
**VERIFICATION RULE:**
```
Final line count MUST be ≥ max(our_original_lines, main_original_lines)
Example:
├── Our version was: 245 lines
├── Main's version was: 312 lines
├── Expected minimum: 312 lines (max of both)
├── Actual merged: ??? lines
└── Status: ✅ PASS or 🚩 FAIL
```
**If line count is LESS than expected:**
- 🚩 STOP - Something was likely lost
- Re-analyze what's missing
- Ask user before proceeding
### Step 4.2: Feature Presence Verification
Search for every feature identified in Phase 1:
```bash
# Verify features from our branch exist
grep -n "ourFeatureName" <merged-file>
# Verify features from main exist
grep -n "mainFeatureName" <merged-file>
```
**Create verification checklist:**
```
FEATURE VERIFICATION CHECKLIST:
Our Features:
├── [x] ourImport1 - Found at line 5
├── [x] ourStateVariable - Found at line 23
├── [x] ourFunction - Found at line 45
└── [x] ourUIComponent - Found at line 89
Main's Features:
├── [x] mainImport1 - Found at line 7
├── [x] mainStateVariable - Found at line 25
├── [x] mainFunction - Found at line 67
└── [x] mainUIComponent - Found at line 102
Status: ✅ ALL FEATURES PRESERVED
```
### Step 4.3: Syntax and Import Verification
```bash
# Check for TypeScript/JavaScript errors
npx tsc --noEmit <file> 2>&1 || true
# Check for duplicate imports
grep "^import" <file> | sort | uniq -d
```
### Step 4.4: Final Report
```
═══════════════════════════════════════════════════════════════
MERGE VERIFICATION REPORT
═══════════════════════════════════════════════════════════════
FILES MERGED: [count]
LINE COUNT VERIFICATION:
├── [filename]: [original_max] → [final] ✅/🚩
└── ...
FEATURE VERIFICATION:
├── Our features preserved: [x/y] ✅/🚩
└── Main features preserved: [x/y] ✅/🚩
OVERALL STATUS: ✅ SAFE TO COMMIT / 🚩 REVIEW NEEDED
═══════════════════════════════════════════════════════════════
```
---
## PHASE 5: COMMIT
Only after Phase 4 verification passes:
```bash
git add -A
GIT_EDITOR=false git commit -m "merge: integrate changes from main
- Preserved our features: [list key features]
- Integrated main features: [list key features]
- Resolved X conflicts with full feature preservation"
```
---
## WHEN TO ASK QUESTIONS
**ALWAYS ask the user when:**
1. **Line count decrease**: Final file is smaller than the larger original
2. **Feature ambiguity**: You can't determine if two features conflict or complement
3. **Logic conflicts**: Two implementations do the same thing differently
4. **Missing context**: You don't understand what a piece of code does
5. **Uncertainty**: ANY doubt about whether you're doing the right thing
**Question format:**
```
🤔 I need clarification before proceeding:
SITUATION:
[Describe what you're seeing]
MY UNDERSTANDING:
[Explain your interpretation]
MY CONCERN:
[Explain what might go wrong]
OPTIONS:
A) [First option and its implications]
B) [Second option and its implications]
C) [More info needed - explain what would help]
What would you like me to do?
```
---
## CHAIN-OF-THOUGHT REASONING PROTOCOL
At each decision point, think through:
1. **What am I trying to accomplish?** (Merge both sets of changes safely)
2. **What could go wrong?** (Losing code, breaking features)
3. **What evidence do I have?** (Line counts, feature detection results)
4. **What's the safest path?** (Usually: keep everything from both sides)
5. **Am I certain?** (If no, ASK THE USER)
---
## SELF-CORRECTION CHECKLIST
Before finalizing ANY merge resolution, verify:
- [ ] Did I check line counts? (🚩 if main has 50+ more lines)
- [ ] Did I identify ALL features on both sides?
- [ ] Did I show what would be DELETED for each option?
- [ ] Did I get user confirmation for conflict resolution?
- [ ] Is my final line count ≥ max of both originals?
- [ ] Can I find every identified feature in the merged result?
- [ ] Did I use `--theirs` or `--ours`? (🚩 if yes - UNDO)
---
## EMERGENCY RECOVERY
If something goes wrong:
```bash
# Abort merge in progress
git merge --abort
# Reset to before merge
git reset --hard HEAD
# If already committed, undo commit
git reset --soft HEAD~1
```
---
Remember: **It's ALWAYS better to ask a question than to lose code.** When in doubt, stop and ask!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment