Configure Language-Aware Diff Drivers
Set up diff=language attributes in .gitattributes to get function-level diff headers for Python, Java, Go, Rust, and other languages.
Detailed Explanation
Language-Aware Diff Drivers
Git has built-in support for language-specific diff drivers that improve the readability of git diff output. Instead of generic context lines, the diff hunk headers show the function, method, or class that contains the change.
Configuration
# Language-specific diffs
*.py text diff=python
*.java text diff=java
*.kt text diff=kotlin
*.go text diff=golang
*.rs text diff=rust
*.rb text diff=ruby
*.php text diff=php
*.c text diff=cpp
*.cpp text diff=cpp
*.h text diff=cpp
*.cs text diff=csharp
*.html text diff=html
*.css text diff=css
*.md text diff=markdown
Before and After
Without diff=python:
@@ -42,7 +42,7 @@
return result
With diff=python:
@@ -42,7 +42,7 @@ def calculate_total(items):
return result
The function name calculate_total(items) appears in the hunk header, making it immediately clear which function was modified.
Supported Languages
Git includes built-in word-regex patterns for these languages:
| Driver | File types |
|---|---|
python |
.py, .pyx |
java |
.java, .gradle |
kotlin |
.kt, .kts |
golang |
.go |
rust |
.rs |
ruby |
.rb, .rake |
php |
.php |
cpp |
.c, .cpp, .h, .hpp |
csharp |
.cs |
html |
.html, .htm |
css |
.css, .scss |
markdown |
.md |
Custom Diff Drivers
You can define custom drivers in .gitconfig:
[diff "swift"]
xfuncname = "^[ \t]*(class|struct|enum|protocol|func|extension)[ \t].*$"
Then reference it in .gitattributes:
*.swift text diff=swift
Use Case
Any development team that reviews code via `git diff` or pull request diffs benefits from language-aware diff drivers. The improved hunk headers make code reviews faster by immediately showing the function context of each change.