Skip to content

Instantly share code, notes, and snippets.

@maciej
Created July 2, 2013 13:19
Show Gist options
  • Save maciej/5909238 to your computer and use it in GitHub Desktop.
Save maciej/5909238 to your computer and use it in GitHub Desktop.
Change the author & committer name and/or email of a single commit.
#!/bin/sh
#
# Change the author & committer name and/or email of a single commit.
#
# change-author [-f] commit-to-change [branch-to-rewrite [new-name [new-email]]]
#
# If -f is supplied it is passed to "git filter-branch".
#
# If <branch-to-rewrite> is not provided or is empty HEAD will be used.
# Use "--all" or a space separated list (e.g. "master next") to rewrite
# multiple branches.
#
# If <new-name> (or <new-email>) is not provided or is empty, the normal
# user.name (user.email) Git configuration value will be used.
#
# Based on http://stackoverflow.com/questions/3042437/change-commit-author-at-one-specific-commit
force=''
if test "x$1" = "x-f"; then
force='-f'
shift
fi
die() {
printf '%s\n' "$@"
exit 128
}
targ="$(git rev-parse --verify "$1" 2>/dev/null)" || die "$1 is not a commit"
br="${2:-HEAD}"
TARG_COMMIT="$targ"
TARG_NAME="${3-}"
TARG_EMAIL="${4-}"
export TARG_COMMIT TARG_NAME TARG_EMAIL
filt='
if test "$GIT_COMMIT" = "$TARG_COMMIT"; then
if test -n "$TARG_EMAIL"; then
GIT_AUTHOR_EMAIL="$TARG_EMAIL"
GIT_COMMITTER_EMAIL="$TARG_EMAIL"
export GIT_AUTHOR_EMAIL
export GIT_COMMITTER_EMAIL
else
unset GIT_AUTHOR_EMAIL
fi
if test -n "$TARG_NAME"; then
GIT_AUTHOR_NAME="$TARG_NAME"
GIT_COMMITTER_NAME="$TARG_NAME"
export GIT_AUTHOR_NAME
export GIT_COMMITER_NAME
else
unset GIT_AUTHOR_NAME
fi
fi
'
git filter-branch $force --env-filter "$filt" -- $br
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment