Git Merge Conflicts and git-write-tree Errors: In-depth Analysis and Solutions

Dec 07, 2025 · Programming · 12 views · 7.8

Keywords: Git merge conflicts | git-write-tree error | git reset --mixed

Abstract: This article provides a comprehensive analysis of common merge conflict issues in Git version control systems, particularly focusing on the 'fatal: git-write-tree: error building trees' error that occurs after operations like git pull or git revert. The paper first examines the root cause of this error—unresolved merge conflicts in the index preventing Git from constructing valid tree objects. It then explains in detail how the git reset --mixed command works and its differences from git reset --hard. Through practical case studies, the article demonstrates how to safely reset the index state without losing working directory changes, while providing complete troubleshooting procedures and best practice recommendations to help developers effectively manage Git repository states.

Git Merge Conflicts and Index State Anomalies

In distributed version control systems, Git is renowned for its powerful branching and merging capabilities. However, when multiple developers modify the same files simultaneously, merge conflicts become an inevitable common issue. Particularly after executing operations like git pull or git revert, if unresolved conflicts exist, Git may report the "fatal: git-write-tree: error building trees" error, indicating that Git cannot construct valid tree objects to represent the current index state.

Deep Analysis of Error Causes

When Git executes the git write-tree command, it reads the contents of the index and constructs corresponding tree objects. The index is one of Git's core data structures, storing the file states of the staging area. If the index contains files marked as "needs merge" or "unmerged," this indicates unresolved merge conflicts exist, preventing Git from determining the final versions of these files and thus unable to build a consistent tree structure.

In the provided case, we can see multiple files simultaneously displaying "needs merge" and "unmerged" states:

Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx: needs merge
Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx: unmerged (2aafac967c35fa4e77c3086b83a3c102939ad168)
...

This state indicates: 1) files have merge conflicts requiring manual resolution; 2) Git has recorded different versions involved in the conflicts (identified by hash values). When the index is in such an inconsistent state, any operation requiring tree object construction (such as committing, resetting, etc.) will fail.

Solution: git reset --mixed

For this type of index state anomaly, the most effective solution is using the git reset --mixed command. This command performs the following operations:

  1. Resets the HEAD pointer to the specified commit (defaults to current commit)
  2. Resets the index (staging area) to match HEAD's content
  3. Preserves all changes in the working directory

Compared to git reset --hard, the key advantage of --mixed mode is that it does not discard modifications in the working directory. This means even if the index is in a chaotic state, your work成果 remain safe.

The complete execution process is as follows:

# First check current status
git status

# If confirmed need to reset index, execute
git reset --mixed

# Or specify particular commit
git reset --mixed HEAD~1

# Check status again after reset
git status

Technical Implementation Principles

The working principle of git reset --mixed involves multiple levels of Git's internal data structures:

1. HEAD Pointer Operation: Git first moves the HEAD pointer to the target commit. By default (without specifying a commit), HEAD remains unchanged, but the index gets reset.

2. Index Reconstruction: Git reads the tree object corresponding to the target commit, then rebuilds the index based on this tree. This process clears all unresolved merge conflict markers, restoring the index to a consistent state.

3. Working Directory Preservation: Git intentionally does not modify files in the working directory, ensuring developers' local changes are not lost. This is the core difference between --mixed mode and --hard mode.

From an implementation perspective, Git's reset command is defined in the builtin/reset.c source file. When the --mixed option is specified, Git calls the reset_index function, which is responsible for updating the index to match the target tree's state while keeping the working directory unchanged.

Complete Troubleshooting Procedure

When encountering the "git-write-tree: error building trees" error, it is recommended to follow this systematic procedure:

  1. Diagnose the Problem: First use git status to view current status, confirming whether unresolved merge conflicts exist.
  2. Backup Important Changes: Although git reset --mixed does not lose working directory changes, as a best practice, it is recommended to first commit or stage important modifications.
  3. Execute Reset: Run git reset --mixed to clear conflict states from the index.
  4. Re-evaluate: After resetting, use git diff to examine changes in the working directory and decide next steps.
  5. Resolve Conflicts: Manually resolve remaining conflicts, then use git add to add resolved files to the index.
  6. Complete Operation: Finally execute git commit to complete the merge or revert operation.

Preventive Measures and Best Practices

To avoid frequently encountering such issues, developers can adopt the following preventive measures:

1. Regular Pull Updates: Avoid not pulling remote changes for extended periods, reducing the likelihood of large-scale merge conflicts.

2. Use Feature Branches: Create independent branches for each new feature or fix, merging through Pull Requests when complete, facilitating conflict management.

3. Understand Merge Strategies: Learn about Git's different merge strategies (recursive, octopus, ours, etc.), selecting appropriate strategies based on project requirements.

4. Utilize Tool Assistance: Use graphical Git clients or IDE integrated tools, which typically provide more intuitive conflict resolution interfaces.

5. Team Collaboration Standards: Establish clear team collaboration processes, including code review, merge timing, and conflict resolution responsibility allocation.

Extended Discussion: Other Related Commands

Besides git reset --mixed, Git provides other tools for handling merge conflicts:

git mergetool: Invokes configured merge tools (like vimdiff, kdiff3, etc.) to help resolve conflicts.

git checkout --ours/--theirs: Selects to keep "our" or "their" version in conflicted files.

git stash: Temporarily saves working directory changes, cleans up state, then attempts merge operations again.

However, when the index is in a completely chaotic state, git reset --mixed is typically the most direct method to restore a controllable state.

Conclusion

The "fatal: git-write-tree: error building trees" error essentially represents inconsistent Git index state, usually caused by unresolved merge conflicts. By using the git reset --mixed command, developers can safely reset the index without losing changes in the working directory, creating a clean foundation for subsequent conflict resolution. Understanding how this command works and its differences from git reset --hard is crucial for effectively managing Git repository states. In practical development, combining systematic troubleshooting procedures with preventive best practices can significantly reduce the frequency and impact scope of such issues.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.