Last updated 13 October 2026
Use Claude Code with git merge conflicts
The challenge of git conflict markers in automated sessions
You are staring at a terminal full of git conflict markers because a merge or rebase went wrong. When you run Claude Code to fix these issues, the agent often struggles with the raw text of the markers. The Edit tool is designed to find specific blocks of code and replace them with new versions. When a file contains markers like HEAD and incoming branch labels, the surrounding context of the code changes. This causes the Edit tool to fail because it cannot find a clean match for the lines it wants to modify. The agent might try several times to apply a diff and get stuck in a loop of failed attempts.
This problem is compounded by the way git expands the file size and structure during a conflict. A file that was fifty lines long might now be seventy lines long with repeated sections of logic. If you do not give the agent a clear strategy, it may try to fix one small part of the conflict while leaving the markers elsewhere, which leads to syntax errors that break your build. You need a systematic approach to help the agent clean up the mess without exhausting your session context or your patience.
Identify unmerged files using the Bash tool
The first step is to get a clear list of every file that needs attention. Do not let the agent guess which files are broken by looking at the file tree. Instead, instruct the agent to use the Bash tool to run a git command that specifically lists unmerged paths. Running git status with the porcelain flag is a reliable way for the agent to get a machine-readable list of files in the conflict state. This ensures the agent has a complete checklist before it starts any work.
Once the agent has this list, it can use the Glob tool to look for other related files that might be affected by the changes. Often, a conflict in one file implies that a related test file or a configuration file also needs a logic update. By using the Bash tool to get the ground truth from the git index, you prevent the agent from missing hidden conflicts that do not show up in a simple directory listing. This creates a solid foundation for the resolution process.
Prioritize the Read and Write cycle over the Edit tool
When dealing with conflict markers, the Write tool is often more reliable than the Edit tool. The Edit tool tries to be surgical, but conflict markers make the file structure messy. It is better to have the agent use the Read tool to pull the entire contents of the conflicted file into its context. Once the agent has the full text, it can process the logic of both the current changes and the incoming changes in its own memory. This allows it to construct a clean, final version of the file that resolves the logic of both branches.
After the agent has figured out the correct resolution, tell it to use the Write tool to overwrite the entire file. This completely removes the conflict markers in one step and ensures the file is syntactically correct. Overwriting the file is safer than trying to patch it multiple times with the Edit tool, as each patch attempt risks tripping over the remaining markers. This approach keeps the file state clean and reduces the number of tool calls the agent needs to make.
Use project instructions in CLAUDE.md to standardize resolutions
If your project has specific rules for how to handle certain types of conflicts, you should record these in the CLAUDE.md file at the root of your repository. For example, you might always prefer the version of a configuration file from the main branch, or you might have a specific order for importing modules that must be maintained. By placing these rules in CLAUDE.md, you ensure that every session of Claude Code follows the same logic when it encounters a conflict.
The agent reads CLAUDE.md at the start of the session and uses it as a guide for all tool use. You can add a section specifically for conflict resolution that tells the agent to always run a specific build command after resolving a file. This prevents the agent from assuming its work is done just because the markers are gone. It forces the agent to verify that the resulting code actually works within the context of your specific project structure.
Monitor multiple conflict resolutions with adhdTerminal
When you are dealing with a large rebase that affects multiple microservices or modules, managing everything in a single terminal window is difficult. You can use adhdTerminal to run many Claude Code sessions side by side, with each tile representing a different part of the project or a different branch you are trying to clean up. This visual layout allows you to see the status of each resolution at a glance, so you know which files are done and which ones are still waiting for your input.
The adhdTerminal wall shows you the last exchanges of each conversation in its own tile, which is helpful when you are waiting for the agent to finish a long sequence of Read and Write operations. If one tile gets stuck because of a particularly complex logic conflict, you can focus on that specific session without losing track of the other successful merges happening in the adjacent tiles. This setup provides the visual clarity needed to manage high-pressure git operations across a large codebase.
Switch to plan mode for high stakes logic changes
Not every conflict is a simple choice between two versions of a line. Some conflicts involve deep logic changes where both branches have moved in different directions. In these cases, you should use Shift+Tab to switch the agent into plan mode. In this mode, the agent will describe how it intends to resolve the conflict before it uses any tools to modify your files. This gives you a chance to review its reasoning and correct its course before it makes changes that might be hard to undo.
Plan mode is especially useful when the agent needs to combine logic from both sides of a merge. You can ask the agent to explain why it thinks a particular combination of code is correct. Once you are satisfied with the plan, you can switch back to the default mode or acceptEdits mode to let the agent execute the changes. This extra layer of verification is essential for maintaining code quality during complex merges where the automated tools might otherwise make incorrect assumptions about your intent.
Manage session context and noise with slash commands
Conflict resolution generates a lot of data in the session context. Every time the agent reads a file and attempts to fix it, the conversation grows longer. If the agent makes several failed attempts to use the Edit tool, the context window can quickly become cluttered with useless diffs and error messages. You should use the /compact command to summarize the work done so far. This clears out the detailed history of failed attempts while keeping the important context of what has been achieved.
If a session becomes completely overwhelmed by a difficult conflict, do not be afraid to use the /clear command to start over in the same folder. You can also use the /status command to see which files the agent thinks it has modified and what the current state of the repository is. Keeping the context clean is the best way to ensure the agent remains fast and accurate. A bloated context window often leads to the agent hallucinating or repeating the same mistakes over and over.
Verify the resolution with the Bash tool and tests
Once the agent claims to have resolved all conflicts, you must verify the state of the repository. Instruct the agent to use the Bash tool to run your test suite or build command. It is not enough for the code to be free of markers; it must also be functional. If the tests fail, the agent can use the output of the failure to identify which part of the merge was handled incorrectly. This loop of fixing and testing is the only way to be certain the conflict is truly resolved.
Finally, have the agent use the Bash tool to check git status one last time. This confirms that all files are staged or ready to be committed. You can then use the /review command to have the agent look over its own changes and provide a summary of what was modified. This final review step helps catch small errors like unused imports or duplicate variables that often slip in during a manual or automated merge process.
Questions people ask
What should I do if the agent keeps failing to save a file with markers?
Stop the agent using the Escape key and tell it to use the Read tool to get the whole file and the Write tool to save a new version. The Edit tool often fails on conflict markers because the diff context is ambiguous. Overwriting the entire file is the most reliable way to clear the markers and ensure the code is clean.
How can I see how much a long conflict resolution session is costing?
Use the /cost or /usage slash commands at any time during the session. These commands will show you the current spend and token usage for the active session. If a merge is taking a long time, monitoring these figures helps you decide if it is more efficient to finish the task manually or continue with the agent.
Can I prevent the agent from automatically committing resolved conflicts?
Yes, you can control the agent's behavior by switching permission modes with Shift+Tab. In the default mode, the agent will ask for permission before running commands that modify your repository. You can also set specific permissions in your .claude/settings.json file to require manual approval for every tool use, ensuring you have total control over the final commit.
Read next
- Access git submodules in Claude CodeFix issues where Claude Code cannot see or modify code located inside nested git submodules within your main repository.
- Allow Claude Code to run shell scriptsResolve tool execution errors by adjusting permission settings to let Claude Code execute local bash or powershell scripts in your terminal.
- Break a linter error loop in ClaudeWhat to do when Claude Code fixes one linting error only to create another, leading to a circular set of edits.
- Change the Node version for Claude CodeResolve compatibility issues by switching the Node.js version used by Claude Code without breaking your other local development tools.