Skip to content

Git Diff for Deployment Decisions ​

Use git diff family tools to detect what changed between releases, decide what to deploy, generate a change log, or gate a promotion.

Why a DevOps Engineer Needs This ​

CI pipelines often rebuild and ship everything on every push. If your repo has 10 microservices in one monorepo, that wastes build minutes and risks deploying services that did not change. The alternative is to compare two revisions and act only on what actually moved.

The commands here are:

  • git diff --name-only -- list changed files between any two trees
  • git diff --diff-filter -- filter by change type (added, modified, deleted, renamed)
  • git diff-tree -- plumbing equivalent for scripting, no working tree needed
  • git diff --exit-code -- predicate that returns 0 if nothing changed
  • git shortlog -- grouped commit summaries for release notes
  • git log --oneline --first-parent -- clean deployment-boundary commit lists

Pattern 1: Targeted Build by Changed Paths ​

A CI job that builds only the service whose sources actually changed.

bash
#!/bin/bash
# ci-targeted-build.sh
set -euo pipefail

BASE="${1:-origin/main}"
HEAD="${2:-HEAD}"

# Files that changed between BASE and HEAD, relative to repo root
CHANGED=$(git diff --name-only "$BASE"..."$HEAD")

# Determine which services need building
BUILD_SERVICES=""

if echo "$CHANGED" | grep -q "^services/auth/"; then
  BUILD_SERVICES="$BUILD_SERVICES auth"
fi
if echo "$CHANGED" | grep -q "^services/api/"; then
  BUILD_SERVICES="$BUILD_SERVICES api"
fi
if echo "$CHANGED" | grep -q "^libs/shared/"; then
  BUILD_SERVICES="$BUILD_SERVICES auth api"
fi

if [ -z "$BUILD_SERVICES" ]; then
  echo "No service sources changed. Skipping build."
  exit 0
fi

for svc in $BUILD_SERVICES; do
  echo "Building services/$svc..."
  docker build -t "svc-$svc:latest" -f "services/$svc/Dockerfile" .
done

Pattern 2: Deploy Gate with --exit-code ​

Exit-code diff as a gate. Only deploy if something meaningful changed.

bash
# Gate: infrastructure changes only trigger deploy
git diff --exit-code "$PREVIOUS_TAG" HEAD -- infrastructure/  && \
  echo "No infra changes, skipping deploy" && exit 0

git diff --exit-code "$PREVIOUS_TAG" HEAD -- deploy/ && \
  echo "No deploy config changes, skipping deploy" && exit 0

# Changes detected -- proceed with deployment
./deploy.sh

--exit-code returns 0 when nothing changed, 1 when anything did. The && chains keep the gate tight: a pipeline stage that exits 0 is skipped on purpose, not by accident.

Pattern 3: Change Type Filtering with --diff-filter ​

Skip deployment if only documentation or tests changed.

bash
CHANGED=$(git diff --name-only --diff-filter=AMR "$PREVIOUS_TAG" HEAD)

# Filter out test and doc-only changes
IMPORTANT=$(echo "$CHANGED" | grep -v -E '\.(md|txt)$' | grep -v -E '^tests/' || true)

if [ -z "$IMPORTANT" ]; then
  echo "Only docs or tests changed. Skipping deploy."
  exit 0
fi

--diff-filter flags:

FlagMeaning
AAdded
CCopied
DDeleted
MModified
RRenamed
TType changed (mode)
UUnmerged
XUnknown

Combine them: --diff-filter=AMR picks additions, modifications, and renames. Uppercase is included, lowercase is excluded: --diff-filter=m means "everything except modified".

Pattern 4: Release Notes from git shortlog ​

Generate a grouped changelog between two tags.

bash
#!/bin/bash
# scripts/gen-changelog.sh
# Usage: ./gen-changelog.sh v2.3.0 v2.4.0

FROM="${1:-$(git tag --sort=-version:refname | head -2 | tail -1)}"
TO="${2:-HEAD}"

cat <<HEADER
# Changelog $FROM -> $TO

HEADER

git shortlog "$FROM"..."$TO" --format="* %s" --no-merges \
  | sed 's/\[.*\] //g'

git shortlog groups commits by author, then lists each commit summary. The --no-merges drops merge commits that carry no unique signal. Pipe through group-by-scope patterns when using Conventional Commits:

bash
git shortlog "$FROM"..."$TO" --format="* %s" --no-merges \
  | grep -i "feat:"   | sort > /tmp/changelog-feat.md
git shortlog "$FROM"..."$TO" --format="* %s" --no-merges \
  | grep -i "fix:"    | sort > /tmp/changelog-fix.md
git shortlog "$FROM"..."$TO" --format="* %s" --no-merges \
  | grep -v -i -E "^(feat|fix):" | sort > /tmp/changelog-other.md

Pattern 5: Tree Comparison with git diff-tree (No Checkout) ​

git diff-tree compares two tree objects without touching the working tree. Use it in CI where you only need the answer, not the files.

bash
# Compare two tags in CI without checking out either
git diff-tree --no-commit-id -r --name-only "$PREVIOUS_TAG" "$NEW_TAG"

# Count changed files
COUNT=$(git diff-tree --no-commit-id -r "$PREVIOUS_TAG" "$NEW_TAG" | wc -l)
echo "$COUNT files changed"

# Exit code = 1 if any change, 0 if identical
git diff-tree --exit-code "$PREVIOUS_TAG" "$NEW_TAG"

-r recurses into subdirectories. --no-commit-id suppresses the raw object SHA. Pipe the output to grep or awk for selective deployment triggers.

Pattern 6: First-Parent Log for Deployment Boundaries ​

git log --first-parent follows only the mainline merge commits, skipping feature-branch detail. This gives you a clean view of what was integrated into a release branch.

bash
# Show every merge into main since the last tag
git log --oneline --first-parent "$LAST_TAG"..main

# Pair with diff to get change context
for COMMIT in $(git log --format="%H" --first-parent "$LAST_TAG"..main); do
  FILES=$(git diff-tree --no-commit-id -r --name-only "$COMMIT")
  echo "Commit $COMMIT changed: $(echo "$FILES" | tr '\n' ' ')"
done

Pattern 7: Git Blame Ignore Revs for Blame Truth ​

git blame is useless when a mass-formatting commit owns every line. Point blame at a file of known skip-worthy SHAs.

Create .git-blame-ignore-revs in the repo root:

# Prettier bulk format -- Oct 15 2024
abc123def456abc123def456abc123def456abc123
# ESLint autofix sweep -- Nov 2 2024
def789abc012def789abc012def789abc012def789

Then configure it:

bash
git config blame.ignoreRevsFile .git-blame-ignore-revs

Now git blame skips those commits and shows the last real author. In CI, this is useful when auditing who changed what for deployment approval decisions.

Pattern 8: Commit Trailers for Automated CI Annotations ​

Structured trailers in commit messages let your pipeline read deployment metadata without parsing prose.

bash
# Write a commit with trailers
git commit -m "fix: handle timeout in payment gateway

Reviewed-by: jane.doe@corp.com
Deploy-Env: staging
Jira: OPS-4422"

# Extract a trailer in a script
TICKET=$(git log -1 --format="%(trailers:key=Jira,valueonly)")
echo "Deploying for ticket: $TICKET"

# Check if commit is aimed at production
ENV=$(git log -1 --format="%(trailers:key=Deploy-Env,valueonly)")
if [ "$ENV" = "production" ]; then
  ./deploy-prod.sh
fi

git interpret-trailers also writes trailers programmatically:

bash
echo "feat: add health endpoint" | git interpret-trailers \
  --trailer "Signed-off-by: CI Bot <ci@corp.com>" \
  --trailer "Deploy-Env: staging"

Pattern 9: Environment Promotion Gate ​

Check that a commit deployed to staging is the same one being promoted to production, using tree hashes.

bash
STAGING_SHA=$(git rev-parse origin/release/staging)
PROD_SHA=$(git rev-parse origin/release/production)

if [ "$STAGING_SHA" = "$PROD_SHA" ]; then
  echo "No new commits to promote."
  exit 0
fi

# Verify staging tree is an ancestor of the candidate (fast-forward check)
git merge-base --is-ancestor "$STAGING_SHA" "origin/release/production" && \
  echo "Fast-forward possible, promoting..." && \
  git push . origin/release/staging:origin/release/production

Pattern 10: Monorepo Deploy Matrix from Diff ​

Build a deploy matrix for N services from a single diff pass.

bash
#!/bin/bash
set -euo pipefail

CHANGED=$(git diff --name-only "$1" "$2")
SERVICES=("api" "web" "worker" "scheduler")
MATRIX="["

for svc in "${SERVICES[@]}"; do
  if echo "$CHANGED" | grep -q "^services/$svc/\|^libs/shared/"; then
    [ "$MATRIX" != "[" ] && MATRIX+=", "
    MATRIX+="\"$svc\""
  fi
done

MATRIX+="]"
echo "::set-output name=services::$MATRIX"

Output feeds a CI matrix job so only affected services get built and deployed.


This is not about how diff feels. It is about what diff says: which files, which types, which boundaries. Pipe the output to decisions.

Git Master — Git Reference for Every Skill Level