Skip to content

Instantly share code, notes, and snippets.

@zfael
Created December 11, 2025 15:07
Show Gist options
  • Select an option

  • Save zfael/2a60b9e26d9a9c376034d692e1483f2c to your computer and use it in GitHub Desktop.

Select an option

Save zfael/2a60b9e26d9a9c376034d692e1483f2c to your computer and use it in GitHub Desktop.
Quick script to compare a given branch (default main) towards the last tag in order to identify the new changes
#!/bin/bash
# compare-release.sh - Compare git tags with branches
# Usage:
# ./compare-release.sh # Compare last tag with main
# ./compare-release.sh --branch develop # Compare with specific branch
# ./compare-release.sh --detailed # Show detailed diff
# ./compare-release.sh --commits-only # Show only commits
set -e
# Default values
BRANCH="main"
SHOW_DETAILED=false
COMMITS_ONLY=false
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Disable git pager for this script
export GIT_PAGER=cat
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--branch)
BRANCH="$2"
shift 2
;;
--detailed)
SHOW_DETAILED=true
shift
;;
--commits-only)
COMMITS_ONLY=true
shift
;;
--help)
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " --branch <name> Compare with specific branch (default: main)"
echo " --detailed Show detailed file diffs"
echo " --commits-only Show only commit messages"
echo " --help Show this help message"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
echo -e "${BLUE}=== Comparing Last Release Tag with ${BRANCH} ===${NC}\n"
# Fetch latest
echo -e "${YELLOW}Fetching latest from remote...${NC}"
git fetch --tags --quiet
git fetch origin ${BRANCH} --quiet
# Get the last tag
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
echo -e "${RED}No tags found in repository${NC}"
exit 1
fi
echo -e "${GREEN}Last released tag: ${LAST_TAG}${NC}"
echo -e "${GREEN}Comparing with: origin/${BRANCH}${NC}\n"
# Count commits
COMMIT_COUNT=$(git rev-list ${LAST_TAG}..origin/${BRANCH} --count)
if [ "$COMMIT_COUNT" -eq 0 ]; then
echo -e "${GREEN}✓ No new commits since last release${NC}"
exit 0
fi
echo -e "${YELLOW}Found ${COMMIT_COUNT} new commits${NC}\n"
# Show commits (--no-pager ensures output is not paginated)
echo -e "${BLUE}=== Commit History ===${NC}"
git --no-pager log ${LAST_TAG}..origin/${BRANCH} \
--pretty=format:"%C(yellow)%h%C(reset) - %C(green)(%ar)%C(reset) %s %C(blue)- %an%C(reset)" \
--abbrev-commit
if [ "$COMMITS_ONLY" = true ]; then
exit 0
fi
# File statistics
echo -e "\n\n${BLUE}=== Files Changed ===${NC}"
git --no-pager diff ${LAST_TAG}..origin/${BRANCH} --stat
echo -e "\n${BLUE}=== Changed Files List ===${NC}"
git --no-pager diff ${LAST_TAG}..origin/${BRANCH} --name-status | while read status file; do
case $status in
A) echo -e "${GREEN}[ADDED]${NC} $file" ;;
M) echo -e "${YELLOW}[MODIFIED]${NC} $file" ;;
D) echo -e "${RED}[DELETED]${NC} $file" ;;
R*) echo -e "${BLUE}[RENAMED]${NC} $file" ;;
*) echo -e "[${status}] $file" ;;
esac
done
# Detailed diff if requested
if [ "$SHOW_DETAILED" = true ]; then
echo -e "\n${BLUE}=== Detailed Diff ===${NC}"
git --no-pager diff ${LAST_TAG}..origin/${BRANCH}
fi
# Summary
echo -e "\n${BLUE}=== Release Summary ===${NC}"
echo -e "Previous version: ${GREEN}${LAST_TAG}${NC}"
echo -e "Target branch: ${GREEN}origin/${BRANCH}${NC}"
echo -e "Total commits: ${GREEN}${COMMIT_COUNT}${NC}"
echo -e "Files changed: ${GREEN}$(git --no-pager diff ${LAST_TAG}..origin/${BRANCH} --name-only | wc -l | tr -d ' ')${NC}"
STATS=$(git --no-pager diff ${LAST_TAG}..origin/${BRANCH} --shortstat)
INSERTIONS=$(echo "$STATS" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' || echo 0)
DELETIONS=$(echo "$STATS" | grep -oE '[0-9]+ deletion' | grep -oE '[0-9]+' || echo 0)
echo -e "Lines added: ${GREEN}+${INSERTIONS}${NC}"
echo -e "Lines removed: ${RED}-${DELETIONS}${NC}"
echo -e "\n${BLUE}=== Authors Contributing ===${NC}"
git --no-pager shortlog ${LAST_TAG}..origin/${BRANCH} -sn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment