Articles
Published · November 30, 2026

Use .gitattributes to Diff Markdown Files Cleanly

One line in .gitattributes, *.md diff=markdown, makes Git diffs of your Markdown notes easier to read. Here is where to put it, how to check it, and how to add word-level diffs.

Also in: Español中文हिन्दी

Add *.md diff=markdown to a .gitattributes file and Git will use its built-in Markdown pattern when it labels each changed chunk in a diff. That label is the hunk header. In a notes repository it's often the most confusing part of the output.

Quick answer

Every chunk in a unified diff starts with a line shaped like @@ -k,l +n,m @@ TEXT. The TEXT part is meant to tell you where in the file you are. Per the gitattributes documentation, it is by default a line that begins with an alphabet, an underscore or a dollar sign. That rule comes from source code, where it usually lands on a function name.

In a Markdown note, almost every sentence begins with a letter. So the header shows some random earlier sentence, not the section you edited. The fix is short. One attribute line:

*.md diff=markdown

If hunk headers are new to you, how to read a unified diff hunk header covers the rest of that line first. This setting changes only how diffs are displayed. Your files and commits stay as they are.

Step-by-step

1. See the problem first

Edit a paragraph deep inside a long note and run git diff. You will get something like this:

@@ -42,7 +42,7 @@ Then restart the sync and check the log.
 The second pass is slower because it re-reads every file.
-Expect about ten minutes.
+Expect about twenty minutes.

The text after the second @@ is just the closest earlier line that starts with a letter. It says nothing about which section you are in.

2. Turn on the built-in Markdown pattern

Git ships built-in hunk-header patterns for many languages, and markdown is on that list. The documentation describes it as suitable for Markdown documents. A built-in pattern still has to be switched on through the attributes mechanism, so create or edit .gitattributes in the top folder of your repository:

*.md diff=markdown
*.markdown diff=markdown

Each line is a path pattern followed by one or more attributes. When several lines match a path, a later line overrides an earlier one, per attribute.

3. Choose where the line lives

You have three places to put it, and they differ in who they affect:

  • `.gitattributes` in the repository. Version-controlled and shared with everyone who clones the repository. This is the right home for a team's notes or docs.
  • `$GIT_DIR/info/attributes`. Affects only your copy of that one repository, and has the highest precedence. Use it when the preference is yours alone.
  • The file named by `core.attributesFile`. Applies to every repository for your user. Its default is $XDG_CONFIG_HOME/git/attributes, or $HOME/.config/git/attributes when that variable is not set.

The attributes file is separate from Git's settings files. If you are unsure which settings live where, system, global and local Git config files lays out the difference.

4. Write your own pattern if you want to

You can also name your own driver and give it a regular expression. In .gitattributes:

*.md diff=notes

Then in your Git config:

[diff "notes"]
	xfuncname = "^(#+ .*)$"

diff.<driver>.xfuncname is, in the words of the diff configuration reference, the regular expression that the diff driver should use to recognize the hunk header. This one picks any line that starts with one or more # characters, which is an ATX heading. Test it on a real note before you rely on it. One catch from the gitattributes page: the config file parser eats one level of backslashes, so any backslash in your pattern has to be doubled.

5. Add word-level diffs for prose

Hunk headers fix the "where". For "what changed inside this paragraph", word by word is easier to read. The git diff options include:

git diff --word-diff
git diff --color-words

By default, words are delimited by whitespace. --color-words is equivalent to --word-diff=color, so changes are shown with colour alone. Word diff works inside the same line-by-line hunks, so it refines the output without replacing it. To change what counts as a word, use --word-diff-regex, or set diff.wordRegex in your config.

Common problems and fixes

The hunk header did not change. Check that the pattern really matches the path. *.md in the top-level .gitattributes matches Markdown files, but a file called notes.txt is untouched. A later line that sets diff to something else wins, because a later line overrides an earlier one. You can ask Git what it applied with git check-attr diff -- notes/today.md.

My regular expression works nowhere. Double the backslashes, as noted above, and keep the pattern anchored with ^ and $ so it matches whole lines.

Every line shows as changed. Usually that's a line-ending change, not an edit. The text attribute tells Git to normalize line endings to LF in the index, which is a separate setting from diff. Line endings in text files explains how to tell the two cases apart.

Word diff is still noisy. It only refines hunks that the normal diff already found. If an edit reflowed a paragraph, the line-based diff reports the whole paragraph as changed. Git's diff.algorithm setting accepts myers, minimal, patience and histogram, and a different choice sometimes pairs lines up more sensibly.

Doing this with Carets

Carets is a native notes and code editor for iPhone, iPad and Mac. It opens .gitattributes the same way it opens any plain text file, and its syntax highlighting makes config files easier to scan. You can keep a repository's notes in a project, tag the ones you edit often, and make the small fixes from this article without leaving the editor. Your Markdown stays plain text, so any Git tool can diff it.

Get Carets for iPhone, iPad and Mac on the App Store.

Frequently Asked Questions

What does diff=markdown do?

It tells Git to use its built-in Markdown pattern when it builds the hunk header, the text after the second @@ in a diff. Without it, Git shows the nearest earlier line that starts with a letter, an underscore or a dollar sign.

Do I need a .gitattributes file in every repository?

No. Put the line in $GIT_DIR/info/attributes to affect only your own copy of one repository, or point core.attributesFile at a file to cover all your repositories.

Does diff=markdown change what is stored in the repository?

No. It only changes how diffs are displayed. Your files and commits stay exactly the same.

How do I see word-level changes in a paragraph?

Run git diff --word-diff, or --color-words for colour only. Words are split on whitespace unless you set diff.wordRegex.