Skip to main content
Back to blogs page

November 4, 2026

Completed Isn't Merged in Claude Code

A Claude Code session marked Completed on a worktree branch that was never merged into main

With worktrees on, the Claude Code desktop app runs each parallel session in its own git worktree. When Claude finishes, the session says Completed. But the code is still on the worktree branch. Nothing merges it back to main.

I kept finding completed tasks that never reached main. So I added a Stop hook. If Claude is in a worktree with uncommitted or unmerged work, it can't just stop. It has to list what's pending and ask me whether to commit and merge. Nothing lands on main without my yes.

The hook

#!/bin/bash
input=$(cat)
gitdir=$(git rev-parse --path-format=absolute --git-dir 2>/dev/null) || exit 0
common=$(git rev-parse --path-format=absolute --git-common-dir)
[ "$gitdir" = "$common" ] && exit 0
echo "$input" | grep -q '"stop_hook_active": *true' && exit 0
base=main
git show-ref --verify --quiet refs/heads/main || base=master
branch=$(git branch --show-current)
worktree=$(git rev-parse --show-toplevel)
if [ -n "$(git status --porcelain)" ] || [ "$(git rev-list --count $base..HEAD)" -gt 0 ]; then
  echo "Not on $base yet. Do not commit or merge. List the pending files, the branch ($branch) and the path ($worktree), and ask the user whether to commit and merge." >&2
  exit 2
fi

Setup

Do this from a normal terminal. Claude Code treats ~/.claude as protected, so edit it yourself.

  1. Save the script as ~/.claude/hooks/require-merge.sh and run chmod +x on it.
  2. Add it to ~/.claude/settings.json.
  3. Run /hooks to confirm it's loaded.
{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "~/.claude/hooks/require-merge.sh" }
        ]
      }
    ]
  }
}

When you reply "commit and merge", Claude needs git access. Put git commit, git merge and git push under "ask" in your permissions, not "deny". Then each command still waits for your approval.

We use this at Datum Brain to run agents in parallel without losing work. If you're setting up something similar, book a call.

Author Image

Posted by

Fahad Siddiqui

Founder, Datum Brain