Claude Code से Git merge conflict सुरक्षित तरीके से हल करें
Claude Code के साथ Git conflict हल करने का सुरक्षित workflow: prompts, real cases, pitfalls, tests और team rules.
Git conflict हल करना सिर्फ <<<<<<<, =======, और >>>>>>> हटाना नहीं है। ये markers केवल बताते हैं कि Git automatic decision नहीं ले पाया। असली काम है दोनों branches की intention बचाना। एक branch ने authorization fix किया हो सकता है, और दूसरी branch ने नई screen, API call और tests जोड़े हों।
Claude Code इस काम में मददगार है क्योंकि यह conflicted files पढ़ सकता है, आसपास का code देख सकता है, Git commands चला सकता है, tests update कर सकता है और final diff explain कर सकता है। फिर भी पूरा judgement AI को नहीं देना चाहिए। अगर prompt सिर्फ “fix conflicts” है, तो code compile हो सकता है, लेकिन security rule या business logic टूट सकता है।
Practical workflow यह है: पहले इंसान priority और scope लिखे, फिर Claude Code editing करे, और अंत में इंसान diff, tests और behavior verify करे। यही pattern ClaudeCodeLab projects में सबसे stable रहा है।
Workflow map
main या release से changes
|
v
Git unmerged files बताता है
|
v
Claude Code written policy के साथ resolve करता है
|
v
human diff, tests और behavior review करता है
|
v
resolved state commit होता है
Non-interactive command के लिए official Claude Code CLI reference में claude -p "query" pattern देखें। Automation के लिए Claude Code hooks reference और Claude Code settings काम आते हैं। Git में पुरानी resolutions reuse करने के लिए Git rerere official feature है।
पहले state check करें
Claude Code को edit करने देने से पहले working tree साफ समझना जरूरी है।
git status --short
git branch --show-current
git diff --name-only --diff-filter=U
आखिरी command सिर्फ unresolved files दिखाती है। इस list को खुद पढ़ें। अगर कोई unexpected file दिखे, पहले कारण समझें, फिर AI को edit करने दें।
Prompt में ये चार बातें जरूर लिखें:
| सवाल | Example policy |
|---|---|
| कौन सा side priority है | main के security fixes और feature की नई UI दोनों बचें |
| क्या edit हो सकता है | केवल unresolved files और directly related tests |
| generated files का rule | lockfile और generated code हाथ से merge नहीं, regenerate करें |
| done कब मानें | markers नहीं, git diff --check clean, tests pass, decisions explain |
यह छोटा policy block Claude Code को guess करने से रोकता है और review आसान बनाता है।
Use case 1: feature branch में main merge करना
सबसे common case है कि feature branch कुछ दिन पुरानी हो गई और pull request से पहले main merge करना है।
git fetch origin
git merge origin/main
cat > /tmp/claude-conflict-plan.md <<'PROMPT'
Current Git merge conflicts resolve करें।
Context:
- Current branch एक feature branch है।
- origin/main के security fixes और type changes जरूर रखें।
- feature branch की new screen, API call और tests भी रखें।
- सिर्फ git diff --name-only --diff-filter=U में आने वाली files पर काम करें।
Tasks:
1. हर file का conflict reason छोटा explain करें।
2. conflict markers हटाकर दोनों intentions integrate करें।
3. जरूरत हो तो सिर्फ directly related tests update करें।
4. git diff --check चलाएं।
5. decisions और commands summarize करें।
Do not:
- unrelated refactor न करें।
- किसी side को silently discard न करें।
- package-lock.json हाथ से edit न करें।
PROMPT
claude -p "$(cat /tmp/claude-conflict-plan.md)"
git diff --check
npm test
इस prompt में exact words से ज्यादा important है priority, scope और completion criteria। Masa की एक गलती में main ने authorization strict किया था और feature ने UI branch जोड़ी थी। Vague prompt ने UI बचाई, लेकिन API 403 दे रही थी। उसके बाद से prompt में security और UI/API consistency साफ लिखता हूं।
Use case 2: rebase conflicts step by step
git rebase conflict merge से ज्यादा sensitive होता है, क्योंकि Git हर commit पर रुकता है। Rebase में ours और theirs का मतलब confusing लग सकता है। इसलिए diff देखे बिना git checkout --ours न चलाएं।
git rebase origin/main
cat > /tmp/claude-rebase-step.md <<'PROMPT'
इस rebase step का current conflict ही resolve करें।
पहले:
- git status से confirm करें कि rebase चल रहा है।
- git diff --name-only --diff-filter=U से unresolved files देखें।
- recent git log से current commit की intention समझें।
- उस intention को main के current behavior से integrate करें।
Constraints:
- git rebase --continue मत चलाएं।
- resolve और git add के बाद रुकें।
- unrelated files edit न करें।
- decision unclear हो तो options explain करें, guess न करें।
PROMPT
claude -p "$(cat /tmp/claude-rebase-step.md)"
git diff --check
npm test
git rebase --continue
Claude Code से पूरा rebase continue भी करवाया जा सकता है, लेकिन beginner के लिए हर step पर रुकना बेहतर है। गलत resolution कई commits आगे चली जाए तो debugging कठिन हो जाती है।
Use case 3: lockfile और generated code
package-lock.json, pnpm-lock.yaml, OpenAPI generated code, Prisma generated files और बड़े snapshots को line by line merge करना risky है। पहले source file resolve करें, फिर output regenerate करें।
cat > /tmp/claude-lockfile-policy.md <<'PROMPT'
Lockfile या generated-file conflicts resolve करें।
Policy:
- पहले package.json, schema, OpenAPI definition जैसी human-authored source files resolve करें।
- package-lock.json हाथ से merge न करें।
- dependencies decide होने के बाद npm install से lockfile regenerate करें।
- generated code source schema resolve होने के बाद regenerate करें।
- regenerated diff expected क्यों है, explain करें।
Allowed:
- npm install
- npm test
- npm run lint
PROMPT
claude -p "$(cat /tmp/claude-lockfile-policy.md)"
npm install
npm test
git add package.json package-lock.json
Common pitfall है कि package.json में दोनों dependencies रख दीं, लेकिन lockfile एक side से ले लिया। Local machine पर चल सकता है, CI में dependency resolution बदल सकता है। Rule लिखें: source पहले, generated output बाद में।
Use case 4: conflict का कारण समझना
Conflict solve होने के बाद root cause analyze करें। अगर routes, permissions, schema या dependencies बार-बार conflict कर रहे हैं, तो PR बड़ा है या ownership unclear है।
cat > /tmp/claude-conflict-retro.md <<'PROMPT'
यह Git conflict क्यों हुआ, analyze करें।
Investigate:
- git diff --name-only --diff-filter=U से files list करें।
- git log --oneline --all -- <file> से दोनों branch history देखें।
- कारण classify करें: shared ownership, oversized PR, generated noise, missing project rule.
Output:
- cause summary तीन lines में।
- CLAUDE.md में जोड़ने वाली rules।
- PR split, owner coordination या tests में से क्या सबसे useful है।
PROMPT
claude -p "$(cat /tmp/claude-conflict-retro.md)"
अगर src/routes.ts हर हफ्ते conflict करता है, route registration को feature-wise split करना चाहिए। अगर package.json बार-बार conflict करता है, dependency changes अलग PR में रखें।
Checks और team rules
Resolution के बाद कम से कम ये commands चलाएं:
git diff --check
git diff --name-only --diff-filter=U
npm test
Project में typecheck, lint या E2E है तो उन्हें भी चलाएं। Repeated checks को hooks से automate करें। Details के लिए Claude Code Hooks Guide देखें।
{
"hooks": {
"PostToolUse": [
{
"matcher": "Bash(git merge*)|Bash(git rebase*)",
"hooks": [
{
"type": "command",
"command": "git diff --check && npm test"
}
]
}
]
}
}
Rules को CLAUDE.md में भी रखें ताकि Claude Code project memory के रूप में पढ़ सके। Structure के लिए CLAUDE.md best practices और team collaboration guide देखें।
## Git conflict policy
- Security fixes, authorization checks और data-loss prevention default रूप से preserve करें।
- UI, API, schema और tests साथ में verify करें।
- lockfiles और generated code हाथ से edit न करें, regenerate करें।
- rebase में git rebase --continue से पहले human diff review करे।
- decision unclear हो तो options दें, किसी side को silently discard न करें।
Pitfalls और test result
ours और theirs को fixed labels की तरह याद न करें; rebase में ये confusing हो सकते हैं। “दोनों रखो” भी हमेशा सही नहीं है। दो validations, दो redirects या दो feature flags duplicate behavior बना सकते हैं। Tests pass होना जरूरी है, लेकिन authentication, billing, migrations और destructive operations में human review भी चाहिए।
Masa के test में करीब 15 conflicted files वाले TypeScript project पर vague prompt ने syntax तो ठीक किया, लेकिन related test update छूट गया। इसी article वाली policy देने पर diff छोटा हुआ, decisions साफ मिले और review faster हुआ।
छोटी feature branch से शुरुआत करें: unresolved files list करें, clear policy दें, tests चलाएं और diff खुद देखें। जब process stable हो जाए, वही rules hooks और CLAUDE.md में डाल दें ताकि team conflict resolution repeatable workflow बन सके।
मुफ़्त PDF: Claude Code cheatsheet
Email डालें और commands, review habits तथा safe workflow वाली एक-page PDF पाएँ.
हम आपका data सुरक्षित रखते हैं और spam नहीं भेजते.
लेखक के बारे में
Masa
Claude Code workflow और team adoption पर काम करने वाला engineer.
संबंधित लेख
Claude Code permission safety ladder: access धीरे-धीरे बढ़ाएं
read-only से limited edits, proof commands और deploy checks तक permission बढ़ाने की सुरक्षित ladder.
Claude Code Small PR Proof Pack: छोटे PR को review-ready बनाना
Claude Code PR के लिए diff, checks, public URL, CTA path और rollback वाला practical proof pack.
Claude Code Review Gate Before Commit: diff, test, public URL और CTA जांच
Claude Code से commit से पहले review gate बनाएं: diff, build, public URL, Gumroad, consultation, tests और unrelated files।