Skip to content

Instantly share code, notes, and snippets.

@VladimirCores
Created December 4, 2025 08:24
Show Gist options
  • Select an option

  • Save VladimirCores/fbb67b8c7c1076ec818a1e19956db7ee to your computer and use it in GitHub Desktop.

Select an option

Save VladimirCores/fbb67b8c7c1076ec818a1e19956db7ee to your computer and use it in GitHub Desktop.
Extracts commits - sudo sh ./extract_commits.sh PAKDEV-1679 commits.txt
#!/bin/bash
# This script extracts all commit subjects from the current HEAD
# until it finds the first commit whose message starts with the provided pattern.
# It can optionally extract only the unique ticket numbers from merge requests.
# The output is saved to a specified file.
# Check if enough arguments were provided.
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <commit-message-start-pattern> <output-file> [--tickets-only]"
echo "Example: $0 PAKDEV-1234 commit_messages.txt"
echo "Example: $0 PAKDEV-1234 tickets.txt --tickets-only"
exit 1
fi
PATTERN="$1"
OUTPUT_FILE="$2"
TICKETS_ONLY=false
# Check for the optional third parameter
if [ "$3" == "--tickets-only" ]; then
TICKETS_ONLY=true
fi
# Find the commit hash of the most recent commit with a message starting with the pattern.
# The search is done from HEAD backwards.
COMMIT_HASH=$(git log --grep="^${PATTERN}" -n 1 --pretty=format:"%H")
# Check if a commit with that message was found.
if [ -z "$COMMIT_HASH" ]; then
echo "Error: Could not find a commit with a message starting with '${PATTERN}'."
exit 1
fi
echo "Found commit hash: ${COMMIT_HASH}"
echo "Extracting commit messages from ${COMMIT_HASH}..HEAD"
# Log all the merge commit subjects from the found commit up to the current HEAD and save to a file.
if [ "$TICKETS_ONLY" = true ]; then
echo "Extracting unique ticket numbers only to ${OUTPUT_FILE}."
git log --merges --pretty=format:"%s" "${COMMIT_HASH}"..HEAD | cut -d ':' -f 1 | sort -u > "${OUTPUT_FILE}"
else
echo "Saving full merge commit messages to ${OUTPUT_FILE}."
git log --merges --pretty=format:"%s" "${COMMIT_HASH}"..HEAD > "${OUTPUT_FILE}"
fi
echo "Done. Output saved to ${OUTPUT_FILE}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment