show-branch Git Command Guide
The git show-branch command displays a textual representation of the commit topology and branch relationships in a repository. It shows how branches relate to each other and helps visualize the commit history structure, making it useful for understanding complex branch topologies and merge relationships.
git show-branch Syntax:
Section titled “git show-branch Syntax:”git show-branch [<options>] [<rev> | <glob>]...git show-branch (--all | --remotes | --topo-order | --date-order) [--current] [--color[=<when>] | --no-color] [--sparse] [--more=<n> | --list | --independent | --merge-base] [--no-name | --sha1-name] [--topics] [(<rev> | <glob>)...]Display Control Options:
Section titled “Display Control Options:”| Option | Description |
|---|---|
--all | Show all refs under refs/ |
--remotes | Show remote-tracking branches |
--current | Include current branch |
--color[=<when>] | Color output control |
--no-color | Disable color output |
--sparse | Suppress commits not on all branches |
--more=<n> | Show additional commits |
--list | Synonym for —more=-1 |
--merge-base | List merge bases for the given commits |
--independent | List refs with no common commits |
--no-name | Don’t show ref names |
--sha1-name | Name commits with unique prefix of SHA-1 |
Ordering Options:
Section titled “Ordering Options:”| Option | Description |
|---|---|
--topo-order | Show commits in topological order |
--date-order | Show commits in date order |
--topics | Show only commits not on the first branch |
Output Control Options:
Section titled “Output Control Options:”| Option | Description |
|---|---|
-g, --reflog | Show reflog information |
--decorate[=<format>] | Decorate refs with additional info |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
<rev> | Commit or branch reference |
<glob> | Glob pattern for refs |
Understanding Show-Branch Output:
Section titled “Understanding Show-Branch Output:”Basic Output Structure:
Section titled “Basic Output Structure:”Branch Topology Display:* [branch-name] commit subject ! [other-branch] commit subject ! [third-branch] commit subject--- + [branch-name] commit subject + [branch-name^] parent commit ++ [other-branch] commit subjectColumn Markers:
Section titled “Column Markers:”Column Markers:├── * = Commit is on the current branch├── + = Commit is on this branch├── - = Commit is not on this branch├── ! = Branch reference point└── [name] = Branch name or commit referenceBranch Relationship Examples:
Section titled “Branch Relationship Examples:”Simple Branch Structure:* [main] Latest commit on main ! [feature] Latest commit on feature--* [main] Merge commit* [main^] Commit before merge + [feature] Feature commit + [feature^] Base commit
Complex Merge Structure:* [main] Recent main commit ! [feature-a] Feature A head ! [feature-b] Feature B head--- * [main] Merge of A and B + [feature-a] Feature A changes + [feature-b] Feature B changes * [main^] Common ancestorBasic Show-Branch Operations:
Section titled “Basic Show-Branch Operations:”Branch Topology Visualization:
Section titled “Branch Topology Visualization:”# Show current branch topologygit show-branch
# Show specific branchesgit show-branch main feature-branch
# Show multiple branchesgit show-branch main develop feature-1 feature-2
# Show all branchesgit show-branch --all
# Show remote branchesgit show-branch --remotesCommit History Analysis:
Section titled “Commit History Analysis:”# Show recent commit historygit show-branch --more=10
# Show all commits (no limit)git show-branch --list
# Show commits in date ordergit show-branch --date-order
# Show commits in topological ordergit show-branch --topo-orderBranch Relationship Analysis:
Section titled “Branch Relationship Analysis:”# Find merge basesgit show-branch --merge-base main feature
# Show independent branchesgit show-branch --independent main feature bugfix
# Show branch divergencegit show-branch main..featureAdvanced Show-Branch Scenarios:
Section titled “Advanced Show-Branch Scenarios:”Complex Repository Analysis:
Section titled “Complex Repository Analysis:”# Analyze release branchesgit show-branch v1.0 v1.1 v2.0 main
# Compare feature branchesgit show-branch --topics feature-* main
# Analyze hotfix branchesgit show-branch --remotes origin/hotfix-* origin/main
# Show branch creation pointsgit show-branch --merge-base --allReflog Integration:
Section titled “Reflog Integration:”# Show reflog informationgit show-branch -g
# Show reflog for specific branchesgit show-branch -g main feature
# Analyze branch historygit show-branch --reflog --all | head -20Custom Branch Selection:
Section titled “Custom Branch Selection:”# Use glob patternsgit show-branch 'refs/heads/feature/*'
# Show specific remote branchesgit show-branch 'refs/remotes/origin/*'
# Combine local and remotegit show-branch main origin/mainSparse Display:
Section titled “Sparse Display:”# Show only common commitsgit show-branch --sparse main feature
# Limit output depthgit show-branch --more=5 main develop
# Focus on recent activitygit show-branch --more=3 --currentConfiguration and Best Practices:
Section titled “Configuration and Best Practices:”Git Configuration for Show-Branch:
Section titled “Git Configuration for Show-Branch:”# Configure show-branch behaviorgit config showbranch.default "main develop" # Default branches to show
# Configure colorsgit config color.showbranch.current yellowgit config color.showbranch.local greengit config color.showbranch.remote red
# Configure output formatgit config showbranch.defaultFormat "%h %s %d"Show-Branch Best Practices:
Section titled “Show-Branch Best Practices:”# Use for branch analysisgit show-branch --all | head -20
# Check merge relationshipsgit show-branch --merge-base feature main
# Analyze complex topologiesgit show-branch --topo-order --all
# Regular repository inspectiongit show-branch --current --remotesSafe Show-Branch Operations:
Section titled “Safe Show-Branch Operations:”# Check repository state firstgit status
# Limit output for large reposgit show-branch --more=5 --all
# Use specific branchesgit show-branch main develop # Instead of --allIntegration with Development Workflows:
Section titled “Integration with Development Workflows:”Branch Strategy Analysis:
Section titled “Branch Strategy Analysis:”#!/bin/bash# Analyze branch strategy with show-branch
analyze_branches() { echo "=== Branch Strategy Analysis ==="
# Show current branch topology echo "Current branch topology:" git show-branch --current --remotes | head -15
# Analyze branch relationships echo "" echo "Branch relationships:" git show-branch --merge-base --all 2>/dev/null | while read -r line; do echo "Merge base: $line" done
# Check for complex merges echo "" echo "Complex merge analysis:" git log --oneline --graph --all | head -20
# Branch age analysis echo "" echo "Branch age analysis:" git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short) %(committerdate:relative)' | head -10
echo "Analysis complete"}
analyze_branchesMerge Planning:
Section titled “Merge Planning:”# Plan merges using show-branchplan_merge() { local source_branch="$1" local target_branch="${2:-main}"
echo "=== Merge Planning: $source_branch -> $target_branch ==="
# Show branch relationship echo "Branch relationship:" git show-branch "$target_branch" "$source_branch" | head -10
# Find merge base echo "" echo "Merge base:" git show-branch --merge-base "$target_branch" "$source_branch"
# Check for conflicts ahead echo "" echo "Potential conflicts:" git diff --name-only "$target_branch...$source_branch" | head -10
# Show recent commits on source echo "" echo "Recent commits on $source_branch:" git log --oneline "$source_branch" --not "$target_branch" | head -5
echo "Merge planning complete"}
plan_merge "feature/new-api" "develop"Repository Health Check:
Section titled “Repository Health Check:”# Repository health check with show-branchhealth_check() { echo "=== Repository Health Check ==="
# Check branch topology echo "Branch topology health:" if git show-branch --all >/dev/null 2>&1; then echo "✓ Branch topology is valid" else echo "⚠ Branch topology issues detected" fi
# Check for orphaned branches echo "" echo "Orphaned branch check:" git show-branch --independent --all 2>/dev/null | while read -r branch; do echo "Independent branch: $branch" done
# Check merge base validity echo "" echo "Merge base validation:" git show-branch --merge-base --all >/dev/null 2>&1 && echo "✓ Merge bases valid" || echo "⚠ Merge base issues"
# Check reflog health echo "" echo "Reflog health:" git show-branch -g >/dev/null 2>&1 && echo "✓ Reflog accessible" || echo "⚠ Reflog issues"
echo "Health check complete"}
health_checkTroubleshooting Common Issues:
Section titled “Troubleshooting Common Issues:”Complex Output Issues:
Section titled “Complex Output Issues:”# Simplify complex outputgit show-branch --sparse main feature
# Limit output depthgit show-branch --more=3 main develop feature
# Use specific branchesgit show-branch main origin/main # Instead of --allPerformance Issues:
Section titled “Performance Issues:”# Speed up large repository analysisgit show-branch --more=5 --current
# Use specific refsgit show-branch main develop # Limit scope
# Avoid expensive operationsgit show-branch --no-color --current # Reduce formattingDisplay Issues:
Section titled “Display Issues:”# Fix color problemsgit show-branch --color=always | cat
# Handle terminal widthCOLUMNS=120 git show-branch
# Fix encoding issuesgit show-branch --encoding=UTF-8Branch Reference Issues:
Section titled “Branch Reference Issues:”# Fix invalid branch referencesgit show-branch --all 2>/dev/null || echo "Some branches invalid"
# Check branch existencegit show-ref | grep "refs/heads/"
# Validate remote branchesgit ls-remote origin 2>/dev/null | head -5Merge Base Issues:
Section titled “Merge Base Issues:”# Handle missing merge basesgit show-branch --merge-base main feature 2>/dev/null || echo "No common history"
# Check repository connectivitygit fsck --unreachable | grep commit | wc -l
# Validate commit objectsgit cat-file -t HEADReflog Issues:
Section titled “Reflog Issues:”# Handle reflog problemsgit show-branch -g 2>/dev/null || echo "Reflog issues"
# Check reflog sizegit reflog | wc -l
# Clean old reflog entriesgit reflog expire --allReal-World Usage Examples:
Section titled “Real-World Usage Examples:”Development Workflow Integration:
Section titled “Development Workflow Integration:”#!/bin/bash# Development workflow with show-branch
dev_workflow_branch() { echo "=== Development Branch Analysis ==="
# Show current development state echo "Current branch topology:" git show-branch --current --remotes | head -15
# Analyze active feature branches echo "" echo "Active feature branches:" git branch -r | grep "origin/feature" | while read -r branch; do branch_name=$(basename "$branch") commits_ahead=$(git rev-list --count "origin/main..$branch") last_commit=$(git log -1 --format="%ci %s" "$branch") echo "$branch_name: $commits_ahead commits ahead, last: $last_commit" done
# Check merge readiness echo "" echo "Merge readiness check:" git branch -r | grep "origin/feature" | while read -r branch; do branch_name=$(basename "$branch") if git diff --quiet "$branch" "origin/main"; then echo "$branch_name: ✓ Ready to merge" else conflicts=$(git diff --name-only "$branch" "origin/main" | wc -l) echo "$branch_name: ⚠ $conflicts files differ" fi done
# Show recent branch activity echo "" echo "Recent branch activity:" git reflog --since="1 week ago" | grep "checkout:" | head -10
echo "Branch analysis complete"}
dev_workflow_branchRelease Management:
Section titled “Release Management:”# Release branch management with show-branchrelease_management() { echo "=== Release Management Analysis ==="
# Show release branch topology echo "Release branch topology:" git show-branch --remotes | grep -E "(release|tag)" | head -10
# Analyze release stability echo "" echo "Release stability analysis:" git tag --list | tail -5 | while read -r tag; do # Check if tag is on main branch if git merge-base --is-ancestor "$tag" main 2>/dev/null; then echo "$tag: ✓ On main branch" else echo "$tag: ⚠ Not on main branch" fi done
# Check release branch merges echo "" echo "Release branch merge analysis:" git log --oneline --merges --grep="release" | head -5
# Show upcoming releases echo "" echo "Upcoming release branches:" git branch -r | grep "origin/release" | while read -r branch; do branch_name=$(basename "$branch") commits=$(git rev-list --count "$branch") last_update=$(git log -1 --format="%ci" "$branch") echo "$branch_name: $commits commits, last update: $last_update" done
# Release timeline echo "" echo "Release timeline:" git tag --format="%(creatordate:short) %(refname:short)" | sort -r | head -10
echo "Release management analysis complete"}
release_managementCode Review Workflow:
Section titled “Code Review Workflow:”# Code review workflow with show-branchcode_review_workflow() { echo "=== Code Review Branch Analysis ==="
# Show review branch topology echo "Review branch topology:" git show-branch --all | grep -E "(review|pr|pull)" | head -10
# Analyze pull request branches echo "" echo "Pull request analysis:" git branch -r | grep -E "(pr|pull|review)" | while read -r branch; do branch_name=$(basename "$branch") commits=$(git rev-list --count "origin/main..$branch") authors=$(git shortlog -s -n "origin/main..$branch" | wc -l) files_changed=$(git diff --name-only "origin/main..$branch" | wc -l)
echo "$branch_name:" echo " Commits: $commits" echo " Authors: $authors" echo " Files changed: $files_changed"
# Check review status (simplified) if git log --grep="Reviewed-by:" "origin/main..$branch" | grep -q .; then echo " Status: ✓ Reviewed" else echo " Status: ⏳ Awaiting review" fi echo "" done
# Show merge conflicts risk echo "Merge conflict risk assessment:" git branch -r | grep -E "(pr|pull|review)" | while read -r branch; do branch_name=$(basename "$branch") common_files=$(git diff --name-only "origin/main...$branch" | wc -l) echo "$branch_name: $common_files files may conflict" done
echo "Code review analysis complete"}
code_review_workflowRepository Archaeology:
Section titled “Repository Archaeology:”# Repository archaeology with show-branchrepository_archaeology() { echo "=== Repository Archaeology ==="
# Show ancient branch topology echo "Ancient branch topology:" git show-branch --all --more=50 | tail -20
# Find branch creation points echo "" echo "Branch creation analysis:" git for-each-ref --format='%(refname:short) %(creatordate:relative)' refs/heads/ | sort -k2 -n | head -10
# Analyze merge patterns over time echo "" echo "Merge pattern evolution:" for year in {2020..2024}; do merges=$(git log --oneline --merges --since="$year-01-01" --until="$year-12-31" | wc -l) echo "$year: $merges merge commits" done
# Find long-lived branches echo "" echo "Long-lived branches:" git for-each-ref --sort=-committerdate --format='%(refname:short) %(committerdate:relative)' refs/heads/ | awk '$2 ~ /(year|month)/ {print}' | head -5
# Analyze branch naming evolution echo "" echo "Branch naming patterns over time:" git log --all --format='%ci %D' | grep -o 'origin/[^,)]*' | sort | uniq -c | sort -nr | head -10
# Find resurrected branches echo "" echo "Branch resurrection analysis:" git reflog --all | grep "checkout:" | awk '{print $8}' | sort | uniq -c | sort -nr | head -5
echo "Repository archaeology complete"}
repository_archaeologyTeam Collaboration Analysis:
Section titled “Team Collaboration Analysis:”# Team collaboration analysis with show-branchteam_collaboration_analysis() { echo "=== Team Collaboration Analysis ==="
# Show team branch interactions echo "Team branch interactions:" git show-branch --all --more=20 | grep -E "(\[|\+|\-)" | head -15
# Analyze cross-team merges echo "" echo "Cross-team merge analysis:" git log --oneline --merges --all | while read -r line; do hash=$(echo "$line" | cut -d' ' -f1) parents=$(git show --no-patch --format="%P" "$hash") parent1_author=$(git show --no-patch --format="%an" "${parents%% *}") parent2_author=$(git show --no-patch --format="%an" "${parents##* }")
# Check if different teams (simplified) if [[ "$parent1_author" != "$parent2_author" ]]; then echo "Cross-team merge: $line" echo " Authors: $parent1_author, $parent2_author" fi done | head -5
# Show collaboration hotspots echo "" echo "Collaboration hotspots (frequently co-authored files):" git log --name-only --all | grep -v "^$" | sort | uniq -c | sort -nr | head -10
# Analyze pair programming echo "" echo "Pair programming analysis:" git log --all --grep="Co-authored-by:" --oneline | wc -l echo " commits with co-authors"
# Show team branch ownership echo "" echo "Team branch ownership:" git for-each-ref --format='%(refname:short) %(authorname)' refs/heads/ | while read -r branch author; do # Categorize by team (simplified) if [[ "$author" == *"frontend"* ]] || [[ "$author" == *"ui"* ]]; then team="Frontend" elif [[ "$author" == *"backend"* ]] || [[ "$author" == *"api"* ]]; then team="Backend" else team="Other" fi echo "$branch: $team team ($author)" done | sort -k2 | head -10
echo "Team collaboration analysis complete"}
team_collaboration_analysisAutomated Repository Monitoring:
Section titled “Automated Repository Monitoring:”# Automated repository monitoring with show-branchrepository_monitoring() { echo "=== Automated Repository Monitoring ==="
# Monitor branch health echo "Branch health monitoring:" total_branches=$(git branch -a | wc -l) active_branches=$(git branch -r | wc -l) stale_branches=$(git branch -v | awk '$3 ~ /[0-9]+ (month|year)/ {count++} END {print count+0}')
echo "Total branches: $total_branches" echo "Active branches: $active_branches" echo "Potentially stale: $stale_branches"
# Monitor merge activity echo "" echo "Merge activity monitoring:" recent_merges=$(git log --oneline --merges --since="1 week ago" | wc -l) echo "Merges this week: $recent_merges"
# Monitor branch topology complexity echo "" echo "Branch topology complexity:" topology_lines=$(git show-branch --all 2>/dev/null | wc -l) if [ "$topology_lines" -gt 100 ]; then echo "⚠ Complex branch topology ($topology_lines lines)" else echo "✓ Manageable branch topology" fi
# Monitor for problematic branches echo "" echo "Problematic branch detection:" git branch -v | while read -r branch commit rest; do branch=$(echo "$branch" | sed 's/^* //') # Check for very old branches if git log -1 --since="1 year ago" "$commit" >/dev/null 2>&1; then continue else echo "Very old branch: $branch" fi done | head -3
# Generate monitoring report cat > "repo-monitor-$(date +%Y%m%d).txt" << EOFRepository Monitoring Report - $(date)
Branch Statistics:- Total branches: $total_branches- Active branches: $active_branches- Stale branches: $stale_branches
Activity Metrics:- Merges this week: $recent_merges- Topology complexity: $topology_lines lines
Health Status: $([ "$stale_branches" -gt 5 ] && echo "⚠ Attention needed" || echo "✓ Healthy")EOF
echo "✓ Monitoring report generated: repo-monitor-$(date +%Y%m%d).txt"}
repository_monitoringWhat’s the difference between git show-branch and git log —graph?
Section titled “What’s the difference between git show-branch and git log —graph?”git show-branch shows branch topology with column-based layout, while git log —graph shows commit graph with ASCII art. show-branch is better for branch relationships, log —graph for commit flow.
How do I see all branches in a compact format?
Section titled “How do I see all branches in a compact format?”Use git show-branch —all —list to show all branches without topology details.
Can git show-branch show remote branches?
Section titled “Can git show-branch show remote branches?”Yes, use git show-branch —remotes or specify remote branches explicitly.
How do I limit the output depth?
Section titled “How do I limit the output depth?”Use —more=
What’s the —merge-base option for?
Section titled “What’s the —merge-base option for?”—merge-base shows the common ancestor commits between specified branches, useful for understanding where branches diverged.
Can git show-branch work with tags?
Section titled “Can git show-branch work with tags?”Yes, you can include tags in the branch list: git show-branch main v1.0 v2.0
How do I see only the current branch topology?
Section titled “How do I see only the current branch topology?”Use git show-branch —current to focus on the current branch and its relationships.
What’s the —independent option for?
Section titled “What’s the —independent option for?”—independent shows branches that have no commits in common, useful for finding completely separate development lines.
Can git show-branch show reflog information?
Section titled “Can git show-branch show reflog information?”Yes, use -g or —reflog to include reflog entries in the branch topology display.
How do I see branches in date order?
Section titled “How do I see branches in date order?”Use git show-branch —date-order to show commits sorted by commit date rather than topology.
What’s the —topics option for?
Section titled “What’s the —topics option for?”—topics shows only commits that are not on the first specified branch, useful for seeing what other branches contribute.
Can git show-branch handle very large repositories?
Section titled “Can git show-branch handle very large repositories?”Yes, but use limiting options like —more=5 and avoid —all on repositories with hundreds of branches.
How do I see branch creation dates?
Section titled “How do I see branch creation dates?”Use git for-each-ref with show-branch, or check reflog for branch creation events.
What’s the —sparse option for?
Section titled “What’s the —sparse option for?”—sparse suppresses commits that are not reachable from all specified branches, showing only the common history.
Can git show-branch work with glob patterns?
Section titled “Can git show-branch work with glob patterns?”Yes, use glob patterns like ‘refs/heads/feature/*’ to match multiple branches.
How do I see the branch hierarchy?
Section titled “How do I see the branch hierarchy?”The column-based output shows the hierarchy naturally, with * marking current branch and + showing branch membership.
What’s the performance impact of show-branch?
Section titled “What’s the performance impact of show-branch?”Generally fast, but —all on large repos with many branches can be slow. Use specific branch names instead.
Can show-branch detect merge conflicts?
Section titled “Can show-branch detect merge conflicts?”No, it shows topology. Use git diff or git merge —no-commit to detect conflicts.
How do I see branches that need merging?
Section titled “How do I see branches that need merging?”Compare outputs of git show-branch with and without —merge-base to identify branches that have diverged.
Applications of the git show-branch command
Section titled “Applications of the git show-branch command”- Branch Topology Analysis: Understanding complex branch relationships and merge histories
- Merge Planning: Identifying merge bases and potential conflict areas
- Repository Archaeology: Exploring historical branch structures and development patterns
- Release Management: Analyzing release branch relationships and stability
- Code Review: Understanding branch divergence and integration points
- Team Coordination: Visualizing collaborative development workflows
- Repository Maintenance: Identifying stale branches and cleanup opportunities
- Development Strategy: Planning branch management and merge strategies