Skip to content

hookGit and releases

Detect Merge Markers

A Claude Code hook that blocks an edit when Git conflict markers are still sitting in the file the agent just wrote or changed.

npx workspacetemplates add detect-merge-markers --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 870 BFile
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{let j={};try{j=JSON.parse(d)}catch(e){}const f=(j.tool_input||{}).file_path;const fs=require('fs');if(!f||!fs.existsSync(f)||fs.statSync(f).size>2000000)process.exit(0);const L=fs.readFileSync(f,'utf8').split(String.fromCharCode(10));const a=[],b=[];L.forEach((l,n)=>{if(/^<<<<<<< /.test(l))a.push(n+1);if(/^>>>>>>> /.test(l))b.push(n+1)});if(a.length&&b.length){console.error('Merge conflict markers remain in '+f+' (opening line '+a[0]+', closing line '+b[0]+'). Resolve the conflict and delete every marker line before moving on.');process.exit(2)}process.exit(0)})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

Agents sometimes resolve a merge conflict halfway and leave the angle-bracket lines behind. This PostToolUse hook opens the file that was just edited and looks for a line starting with seven less-than signs and another starting with seven greater-than signs.

When both are present it exits with code 2, which feeds the message back to the agent so it cleans the file before continuing. Files over 2 MB, missing files and files with no markers pass without a sound.

Example

you › Resolve the conflict in src/config.ts and keep both new options.

agent › If the agent leaves a line starting with <<<<<<< HEAD in the file, the hook reports the opening and closing line numbers and the agent removes the markers in its next step.

Questions

Will it flag a Markdown heading underlined with equals signs?
No. It needs both an opening and a closing marker line, each followed by a space and a label, so a lone row of equals signs is ignored.
Does it check files the agent only reads?
No. It runs after Edit and Write calls only, and it looks at the single path given in that call.
  • Block Git No Verify

    HookGit and releases

    Free
  • Block Git Reset Hard

    HookGit and releases

    Free
  • Inject Git Branch

    HookGit and releases

    Free
  • Session Git Context

    HookGit and releases

    Free