Understanding Line Changes Bat Detects in Git Integration: The Complete Guide
Bat detects four distinct line change types in Git integration: Added (green +), Modified (yellow ~), RemovedAbove (red ‾), and RemovedBelow (red _).
When the git feature is enabled in the sharkdp/bat repository, the tool analyzes repository diffs for each displayed file and marks individual lines with specific change categories. Understanding these line changes bat detects in Git integration helps developers quickly identify code modifications directly in the terminal output.
The Four Types of Line Changes in Bat's Git Integration
The LineChange enum in src/diff.rs defines the four distinct categories that bat uses to classify modifications. Each variant corresponds to a specific visual indicator in the gutter.
Added Lines
Added lines represent new content present in the working tree but not in the index. These appear with a green + symbol in the left margin.
In src/diff.rs (lines 10-12), the Added variant is defined alongside the detection logic in get_git_diff that identifies when old_lines == 0 && new_lines > 0.
RemovedAbove Lines
RemovedAbove indicates a deletion occurring above the current line number. This special case appears when a file is empty and the first line is removed, displayed with a red ‾ (overline) symbol.
The detection condition in get_git_diff (around lines 58-76 in src/diff.rs) checks for new_lines == 0 && old_lines > 0 && new_start == 0 to trigger this variant.
RemovedBelow Lines
RemovedBelow represents the standard deletion case where content has been removed below the current line number. This displays with a red _ (underscore) symbol in the gutter.
The logic identifies these cases when new_lines == 0 && old_lines > 0 && new_start != 0, distinguishing them from RemovedAbove based on the line position.
Modified Lines
Modified lines exist in both the index and working tree but have altered content. These appear with a yellow ~ (tilde) symbol.
This variant serves as the fallback case in the detection logic, triggered when both old and new lines are present in the diff hunk.
How Bat Detects Line Changes: The Implementation
The core detection logic resides in the get_git_diff function within src/diff.rs (lines 58-76). This function processes Git diff hunks and populates a HashMap<u32, LineChange> where keys represent line numbers and values store the detected change type.
The classification follows these specific conditions:
- Added:
old_lines == 0 && new_lines > 0 - RemovedAbove:
new_lines == 0 && old_lines > 0 && new_start == 0 - RemovedBelow:
new_lines == 0 && old_lines > 0 && new_start != 0 - Modified: All other cases where both old and new lines exist
Rendering Line Changes in the Gutter
Once detected, the LineChanges map is passed to the LineChangesDecoration component, which handles the visual rendering. The mapping between enum variants and display symbols occurs in src/decorations.rs (lines 111-116):
// LineChange enum definition from src/diff.rs
#[derive(Copy, Clone, Debug)]
pub enum LineChange {
Added, // renders as green '+'
RemovedAbove, // renders as red '‾'
RemovedBelow, // renders as red '_'
Modified, // renders as yellow '~'
}
The decoration system caches these styled glyphs to optimize performance when rendering large files with extensive modifications.
Practical Usage Examples
To view line changes in Git-tracked files, enable the Git integration feature:
# Display file with Git change indicators
bat --git src/main.rs
# Or set environment variable for persistent behavior
export BAT_GIT=1
bat src/main.rs
Example output showing all four change types:
1 │ fn main() { │
2 │ println!("Hello, world!"); │
3 │ } │
│ ──────────────────────────────── │
4 + │ // New line added │ ← Added (green +)
5 ~ │ // Modified content │ ← Modified (yellow ~)
6 _ │ // Line below was removed │ ← RemovedBelow (red _)
Summary
- Bat detects four line change types in Git integration: Added, RemovedAbove, RemovedBelow, and Modified.
- The
LineChangeenum is defined insrc/diff.rswith detection logic in theget_git_difffunction. - Added lines show green +, Modified lines show yellow ~, and removed lines display red ‾ (above) or _ (below).
- Visual rendering is handled by
LineChangesDecorationinsrc/decorations.rsusing a cached glyph system.
Frequently Asked Questions
How does bat distinguish between RemovedAbove and RemovedBelow?
Bat distinguishes these variants based on the new_start parameter in the diff hunk. When new_start == 0 (indicating the start of the file), deletions are classified as RemovedAbove and displayed with a red overline (‾). When new_start != 0, deletions are marked as RemovedBelow with a red underscore (_). This logic is implemented in get_git_diff within src/diff.rs.
Can I customize the symbols used for line changes?
The symbols are hardcoded in the LineChangesDecoration implementation in src/decorations.rs (lines 111-116). The glyphs (+, ‾, _, ~) are cached as styled strings during initialization. To customize these symbols, you would need to modify the source code and recompile bat, as there are no configuration options or environment variables exposed for changing these specific indicators.
Does bat show line changes for all files or only tracked files?
Bat only displays line change indicators for files tracked by Git (or other supported version control systems). The get_git_diff function attempts to retrieve diff information from the repository; if the file is untracked or not in a Git repository, the LineChanges map remains empty and no gutter symbols are rendered. You must also ensure the --git flag is enabled (or BAT_GIT=1 is set) for this feature to activate.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →