Skip to content

Instantly share code, notes, and snippets.

@tueda
Last active September 22, 2025 14:11
Show Gist options
  • Select an option

  • Save tueda/bed3f1f5f5ba437b3d1ca4777822faaf to your computer and use it in GitHub Desktop.

Select an option

Save tueda/bed3f1f5f5ba437b3d1ca4777822faaf to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Create an empty commit reusing author, date and message from an existing commit.
#
# Usage:
# git phantom <commit-ish>
#
set -euo pipefail
if [ $# -ne 1 ]; then
echo "Usage: git phantom <commit-ish>" >&2
exit 1
fi
if ! git diff --cached --quiet; then
echo "Error: You have staged changes." >&2
exit 1
fi
commit=$1
if ! git rev-parse --verify -q "$commit" >/dev/null; then
echo "Error: Not a valid commit-ish: $commit" >&2
exit 1
fi
author="$(git show -s --format='%an <%ae>' "$commit")"
author_date="$(git show -s --format='%aI' "$commit")"
commit_message="$(git show -s --format='%B' "$commit")"
args=(--allow-empty --author="$author")
if [ -z "$commit_message" ]; then
args+=(--allow-empty-message -m "")
else
args+=(-m "$commit_message")
fi
GIT_AUTHOR_DATE="$author_date" git commit "${args[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment