A guide to navigating source code using Git commands for read-only exploration
git clone https://github.com/apache/kafka.gitcd kafka
Before diving into navigation commands, let's clarify some key Git concepts.
.git foldergit branchtrunk, my-feature.gitgit branch -rorigin/trunk, origin/4.0# Local branches - you can modify these
git branch
* trunk
my-local-branch
# Remote branches - read-only references
git branch -r
origin/trunk
origin/4.0
origin/3.9
origin/3.8
# All branches together
git branch -a
* trunk # Local branch (current)
my-local-branch # Local branch
remotes/origin/trunk # Remote branch
remotes/origin/4.0 # Remote branch
HEAD~1 = one commit beforeHEAD~5 = five commits beforeorigin/trunk = trunk branch on origingit checkout origin/trunk
git checkout 4.0.0
git checkout abc1234
# Show current branch name
git branch --show-current
# Alternative: shows current branch with asterisk
git branch
# Show current branch with more detail
git status
# Show current branch with last commit info
git log --oneline -1
# Show all branches with current marked
git branch -a
# List all remote branches first
git branch -r
# Switch to a remote branch (creates detached HEAD)
git checkout origin/trunk
git checkout origin/4.0
# Switch to an existing local branch
git checkout trunk
# Fetch latest changes from remote before switching
git fetch origin
# Switch after fetching
git fetch origin && git checkout origin/4.0
# List available tags
git tag -l
# Switch to a specific tag
git checkout 4.0.0
git checkout 3.9.1
# Switch to latest tag matching pattern
git checkout $(git describe --tags --abbrev=0)
# Compare two remote branches
git diff origin/trunk..origin/4.0
# Show only file names that differ
git diff --name-only origin/trunk..origin/4.0
# Show summary of changes
git diff --stat origin/trunk..origin/4.0
# Compare specific file between branches
git diff origin/trunk..origin/4.0 -- build.gradle
# Compare with enhanced options
git diff --word-diff origin/trunk..origin/4.0 -- README.md
# Ignore whitespace changes
git diff -w origin/trunk..origin/4.0 -- build.gradle
# Compare multiple specific files
git diff origin/trunk..origin/4.0 -- build.gradle README.md LICENSE
# Compare all files in a directory
git diff origin/trunk..origin/4.0 -- clients/src/main/java/
# Compare files matching a pattern
git diff origin/trunk..origin/4.0 -- "*.gradle" "*.md"
# Search for text in current branch
git grep "KafkaProducer"
# Search with line numbers
git grep -n "bootstrap.servers"
# Search in specific file types
git grep "StreamsConfig" -- "*.java" "*.scala"
# Case-insensitive search
git grep -i "kafka"
# Search across all remote branches
git grep "ConsumerConfig" $(git branch -r)
# Search in specific branches
git grep "TopologyBuilder" origin/trunk origin/4.0 origin/3.9
# Search with context (3 lines before/after)
git grep -C 3 "bootstrap.servers"
# Search in commit history
git log --all --grep="KIP-"
# Search for when a line was added/removed
git log -S "StreamsConfig" --all --oneline
# Search in specific file's history
git log --all -- clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java
# Basic blame - shows author, commit, and date
git blame build.gradle
# Blame with line numbers
git blame -n clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java
# Blame specific line range
git blame -L 10,20 README.md
# Blame on specific branch/tag
git blame origin/4.0 -- build.gradle
git blame 4.0.0 -- clients/build.gradle
# Complete history of a file
git log --follow -- build.gradle
# File history with diff summary
git log --stat --follow -- README.md
# File history with actual changes
git log -p --follow -- clients/build.gradle
# Show file history up to specific version
git log 4.0.0 --follow -- build.gradle
# Show changes between versions
git log 3.9.0..4.0.0 --follow -- clients/build.gradle
# View file content at specific version
git show 4.0.0:build.gradle
git show origin/trunk:README.md
# Find PR merge commits
git log --grep="Merge pull request" --oneline
git log --grep="KAFKA-" --oneline
# From blame to PR workflow
COMMIT=$(git blame -L 25,25 build.gradle | awk '{print $1}')
git log --merges --ancestry-path $COMMIT..HEAD --oneline
-- tells Git: "Everything after this point is a file path, not a command option or branch name."
# Structure: git [command] [options] [branches] -- [files]
git log --oneline --follow -- build.gradle
git diff origin/trunk..origin/4.0 -- clients/src/
git blame -n -w -- README.md
# Clear intent
git log -- build.gradle
git log -- README.md
git diff HEAD~1 -- clients/
# Without -- (potentially ambiguous)
git log build.gradle
git log README.md
git branch --show-current # Current branch
git branch -r # List remote branches
git tag -l # List tags
git checkout origin/trunk # Switch to remote branch
git checkout origin/4.0 # Switch to version branch
git checkout 4.0.0 # Switch to specific tag
git fetch origin # Update remote references
git diff origin/trunk..origin/4.0 # Compare branches
git diff origin/trunk..origin/4.0 -- build.gradle # Compare specific file
git log origin/trunk..origin/4.0 # Commits unique to 4.0 branch
git diff --name-only origin/trunk..origin/4.0 # Just show changed files
git show origin/trunk:build.gradle # View file on branch
git grep "KafkaProducer" # Search current branch
git grep "StreamsConfig" $(git branch -r) # Search all remote branches
git blame build.gradle # See who changed each line
git log --follow -- clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java
git log -S "bootstrap.servers" -- clients/ # Find when code added/removed
git log --grep="KAFKA-" --oneline # Find JIRA ticket commits
git fetch origingit fetch && git checkout origin/trunk| less to paginate long outputstrunk is main, numbered branches are releasesgit grep "KafkaProducer" first, then narrow down-- for clarity