# Git Worktrees के साथ समानांतर Agents — Coding Agents और AI-Assisted Development

Source: https://www.geekswithgeeks.com/hi/coding-agents/w-parallel

> कई agents एक साथ चलाएँ बिना एक-दूसरे को overwrite किए।

## प्रति agent एक working copy

एक ही folder संपादित करते दो agents एक-दूसरे की files overwrite करेंगे और एक-दूसरे का अधूरा कोड चलाएँगे। मानक उपाय **अलगाव** है: हर agent को **अपनी working directory और branch** मिले। Git **worktrees** यह सस्ते में करते हैं: `git worktree add -b agent-a ../agent-a` उसी repository का नई branch पर दूसरा checkout बनाता है, इतिहास साझा पर files अलग। Cloud agents अलग sandboxes से यही पाते हैं। Agents को **स्वतंत्र कार्यों** (अलग modules, अलग bug fixes) पर समानांतर चलाएँ, हर branch की अलग समीक्षा करें, और एक-एक करके merge करें, हर merge के बाद tests दोबारा चलाकर, क्योंकि स्वतंत्र branches भी अर्थ में टकरा सकती हैं। सीमा आपकी **समीक्षा क्षमता** है, शुरू किए जा सकने वाले agents की संख्या नहीं।

## दो agents, दो worktrees, चलाकर

मैंने यह सादे Python 3 (सिर्फ़ standard library) से चलाया, अस्थायी folder में बनाए throwaway project के साथ। Throwaway repository में अलग working directories वाली दो branches बनती हैं। Agent A एक पंक्ति जोड़ता है, agent B file दोबारा लिखता है, और `main` अछूता रहता है। `git diff --shortstat` हर branch के बदलाव का आकार दिखाता है। यह अस्थायी folder में असली `git` उपयोग करता है, आपका अपना repository कभी नहीं।

```python
import subprocess, tempfile, os

def git(cwd, *args):
    return subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True,
                          env={**os.environ, "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@example.com",
                               "GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@example.com"})

with tempfile.TemporaryDirectory() as base:                      # a throwaway repository, never your real one
    main = os.path.join(base, "repo"); os.makedirs(main)
    git(main, "init", "-q", "-b", "main")
    open(os.path.join(main, "app.py"), "w").write("print('v1')\n")
    git(main, "add", "."); git(main, "commit", "-q", "-m", "initial")

    for name in ("agent-a", "agent-b"):                           # one isolated worktree + branch per agent
        git(main, "worktree", "add", "-q", "-b", name, os.path.join(base, name))
    open(os.path.join(base, "agent-a", "app.py"), "w").write("print('v1')\nprint('feature A')\n")
    open(os.path.join(base, "agent-b", "app.py"), "w").write("print('fixed B')\n")

    print("main still has:", open(os.path.join(main, "app.py")).read().strip())
    for name in ("agent-a", "agent-b"):
        git(os.path.join(base, name), "add", "."); git(os.path.join(base, name), "commit", "-q", "-m", name)
        stat = git(main, "diff", "--shortstat", "main", name).stdout.strip()
        print(f"{name}: {stat}")
    print("branches:", sorted(b.strip("* ").strip() for b in git(main, "branch", "--format=%(refname:short)").stdout.split()))

```

Output:

```
main still has: print('v1')
agent-a: 1 file changed, 1 insertion(+)
agent-b: 1 file changed, 1 insertion(+), 1 deletion(-)
branches: ['agent-a', 'agent-b', 'main']
```

## एक बार में एक branch merge करें

हर merge के बाद tests दोबारा चलाएँ; स्वतंत्र branches भी व्यवहार में टकरा सकती हैं।

**Quiz:** कई agents समानांतर चलाने की सीमित करने वाली बात क्या है?

- [ ] Terminal का रंग
- [ ] उपलब्ध branches की संख्या
- [x] उनके बदलावों की समीक्षा की आपकी क्षमता
- [ ] कुछ भी नहीं

*Answer:* उनके बदलावों की समीक्षा की आपकी क्षमता. अनसमीक्षित बदलाव जोखिम हैं, चाहे कितनी भी तेज़ी से बने हों।
