The Nightmare of Data File Merge Conflicts
If you’ve ever stayed up all night just “inspecting” every bracket in a conflicted package-lock.json file, you’re not alone. Seeing thousands of lines of code mixed with <<<<<<< HEAD symbols is a true visual nightmare, but resolving git conflicts the easy way with Meld can save you hours of manual work.
I clearly remember an old project where the team was pushing hard for a deadline. I merged a feature branch into develop and the composer.lock file had a massive conflict. Instead of using tools, I confidently manually deleted the conflict markers. Because I missed a single comma, I broke the lockfile configuration, causing the CI/CD system to hang for 2 hours. This incident didn’t just slow us down; it frustrated the whole team, much like handling 10 git disasters that developers often face. That’s when I realized: Never use human effort for tasks that Git can do better.
By default, Git compares conflicts line-by-line. This mechanism works perfectly for Java, Python, or C++. However, for strictly structured files like JSON, XML, or Lockfiles, this approach often breaks the syntax after merging, which is why it is essential to standardize commits and code quality within your repository. This is exactly where Git Merge Driver becomes your lifesaver.
What Exactly is a Git Merge Driver?
Think of a Git Merge Driver as a private “referee” you hire to handle data collisions. Instead of letting Git decide and report errors, you specify an external script to automatically resolve conflicts based on your own logic.
Whenever Git detects that a file has been changed on both sides, it checks the .gitattributes file. If that file is assigned a specific Merge Driver, Git will immediately call that driver instead of the default merge tool.
How a Merge Driver Works
A professional driver typically receives 3 input parameters from Git, which is part of the lower-level mechanics that allow you to dissect Git from the inside:
- %O: Original version (Ancestor) – the last common point of the two branches.
- %A: Your current version (Ours).
- %B: The version from the branch being merged in (Theirs).
The driver reads these 3 files, applies logic to merge the data, and overwrites the final result into the %A file.
Detailed Guide to Configuring Git Merge Driver
Step 1: Declare the Driver in Git Configuration
You should configure this at the project (local) level to sync with all members. Open your terminal in the root directory and run the following commands:
git config merge.json-merge.name "Automated JSON merge tool"
git config merge.json-merge.driver "python3 path/to/json_merge_script.py %O %A %B"
In this case, json-merge is the identifier. The driver parameter is the path to the processing script we will write.
Step 2: Assigning Responsibilities in .gitattributes
Create or update the .gitattributes file to “delegate” tasks to the new driver:
*.json merge=json-merge
package-lock.json merge=json-merge
This line tells Git: “When a .json file has a conflict, don’t show a warning—call json-merge immediately to handle it”.
Step 3: Writing a Smart Processing Script
Below is a simple json_merge_script.py example in Python to merge JSON keys:
import sys
import json
def merge_json(base_path, ours_path, theirs_path):
try:
with open(base_path) as f: base = json.load(f)
with open(ours_path) as f: ours = json.load(f)
with open(theirs_path) as f: theirs = json.load(f)
# Logic: Merge keys from both sides, prioritizing the latest changes
result = {**base, **ours, **theirs}
with open(ours_path, 'w') as f:
json.dump(result, f, indent=2)
sys.exit(0) # Success
except Exception:
sys.exit(1) # Failure, let Git handle it manually
if __name__ == "__main__":
merge_json(sys.argv[1], sys.argv[2], sys.argv[3])
A “Quick-Fix” Solution for Lockfiles: Union Merge
If you’re hesitant to write a script, Git provides a built-in union driver. This driver keeps the code lines from both sides when a conflict occurs. This is extremely effective for list files like .gitignore.
To apply it quickly for lockfiles, add this to .gitattributes:
package-lock.json merge=union
yarn.lock merge=union
Note: union might cause the lockfile to be slightly off. You should run npm install again after merging to ensure integrity.
Using Specialized Libraries (Recommended)
Don’t try to reinvent the wheel. The community has developed powerful tools like npm-merge-driver for NodeJS.
npx npm-merge-driver install --global
This tool automatically optimizes package-lock.json, ensuring the merged file is always valid and doesn’t cause installation errors.
Vital Notes When Using Merge Drivers
Based on practical experience, I have 3 tips for you:
- Verify the results: No matter how smart the driver is, always run
npm testor check the syntax withjsonlintafter merging. - Share team configuration: Commit the
.gitattributesfile. For the configuration in.gitconfig, you should write an automated setup script for new members. - Be careful with sensitive data: Avoid letting the script automatically overwrite important security settings in environment configuration files.
Conclusion
Customizing the Git Merge Driver is a small investment that yields big productivity returns. It helps the team eliminate tedious manual tasks and minimize human error. If you want to level up your Git productivity, set up a driver today. Happy merging!

