Skip to content

Instantly share code, notes, and snippets.

@disouzam
Created June 29, 2026 11:55
Show Gist options
  • Select an option

  • Save disouzam/336e7eddc34a108b17ce44e8104abdaf to your computer and use it in GitHub Desktop.

Select an option

Save disouzam/336e7eddc34a108b17ce44e8104abdaf to your computer and use it in GitHub Desktop.
Get markdown summary of merge commits
#!/bin/bash
function gitmd() {
local ref="${1:-HEAD}"
local outfile="$2"
# Colors (terminal only)
local BOLD="\033[1m"
local RESET="\033[0m"
local CYAN="\033[36m"
local YELLOW="\033[33m"
local GREEN="\033[32m"
# Disable colors if exporting
if [[ -n "$outfile" ]]; then
BOLD=""; RESET=""; CYAN=""; YELLOW=""; GREEN=""
: > "$outfile"
fi
commits_printed=0
git log "$ref" --merges --decorate=short \
--date=format:'%Y-%m-%d %H:%M:%S' \
--pretty=format:'__COMMIT__%n%H%n%h%n%d%n%s%n%an%n%ad%n%ar%n%cn%n%cd%n%cr%n%p%n%b%n__END__' |
while IFS= read -r line; do
if [[ "$line" == "__COMMIT__" ]]; then
read full_hash
read short_hash
read decorations
decorations=$(echo "$decorations" | sed 's/^ *(//;s/)$//')
read subject
read author
read author_date
read author_date_relative
read committer
read committer_date
read committer_date_relative
read parents
body=""
while IFS= read -r bodyline && [[ "$bodyline" != "__END__" ]]; do
body+="$bodyline"$'\n'
done
# Trim trailing empty lines
body="$(printf "%s" "$body" | sed ':a;/^\n*$/{$d;N;ba;}')"
commits_printed=$((commits_printed + 1))
{
# Header
if [[ $commits_printed -gt 1 ]]; then
echo -e "<br>\n"
fi
echo -e "# ${BOLD}**[Commit $short_hash] $subject**${RESET}"
if [[ -n "$decorations" ]]; then
echo -e "- ${CYAN}Refs:${RESET} ${YELLOW}$decorations${RESET}"
fi
echo -e "- ${CYAN}<u>**Parents:**</u>${RESET} $parents"
echo -e "- ${CYAN}<u>**Author:**</u>${RESET} $author"
echo -e "- ${CYAN}<u>**Author Date:**</u>${RESET} $author_date ($author_date_relative)"
# Smarter committer display
if [[ "$committer" != "$author" || "$committer_date" != "$author_date" ]]; then
if [[ "$committer" != "$author" ]]; then
echo -e "- ${CYAN}<u>**Committer:**</u>${RESET} $committer"
fi
if [[ "$committer_date" != "$author_date" ]]; then
echo -e "- ${CYAN}<u>**Committer Date:**</u>${RESET} $committer_date ($committer_date_relative)"
fi
fi
# Merge detection
parent_count=$(echo "$parents" | wc -w | tr -d ' ')
if [[ "$parent_count" -gt 1 ]]; then
echo -e "- ${GREEN}<u>**Type:**</u>${RESET} Merge commit"
fi
# Summary
summary=$(git show --stat=80 --format="" "$full_hash" | tail -n 1)
if [[ -n "$summary" ]]; then
echo -e "- ${GREEN}<u>**Summary:**</u>${RESET} $summary"
fi
# Per-file changes (git-style)
echo ""
echo "## Files changed:"
echo -e "\n\`\`\`bash"
git show --stat=80 --format="" "$full_hash" | sed '$d' | sed 's/^/ /'
echo -e "\`\`\`"
echo ""
echo "## Commit message:"
echo -e "\n\`\`\`markdown"
echo "$body"
echo -e "\`\`\`\n"
echo "---"
} | {
if [[ -n "$outfile" ]]; then
cat >> "$outfile"
else
cat
fi
}
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment