Saturday, October 17, 2026

Git: Preventing Automatic Merging of Text Files

For text files, a Git merge (say, a feature branch into main) automatically tries to merge individual files (.py, .cs, .cpp, .json, .xml, etc.). There are scenarios when it is better to force a manual merge for text files. The .gitattributes file allows the merge algorithm used to be configured. There may be some folders in a Git repo where automatic merging is desired and other folders where automatic merging should be verboten.

The standalone -merge option in a .gitattributes file forces a manual merge. An instance of a .gitattributes file containing -merge as the sole attribute value is as follows, where the * is a wildcard matching all files:

* -merge

A repo can contain multiple .gitattributes files controlling the merge behavior at different levels of the folder hierarchy. An example of such a topology is as follows:

AnyRep/
├── .gitattributes
├── Source/
│   ├── main.py
│   └── service.py
└── CapCutProjects/
    ├── .gitattributes
    ├── Project1/
    │   └── draft_info.json
    └── Project2/
        └── draft_info.json

Binary files are not subject to auto-merge (.dll, .exe, .jpg, .png, .zip, .mp4, .wav, etc.).

The documentation for using the merge keyword in .gitattributes can be found at Performing a three-way merge.

I used the following .gitattributes file for a repo containing CapCut projects (JSON files) because I do not want to permit auto-merge.

# -merge never auto-merge void text files (CapCut project files are json/text)
* -text -merge

Why the -text option? That is a topic for a future blog.


No comments :

Post a Comment