Skip to content

Instantly share code, notes, and snippets.

@ChrisCummins
Created July 23, 2013 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisCummins/6061579 to your computer and use it in GitHub Desktop.
Save ChrisCummins/6061579 to your computer and use it in GitHub Desktop.
It was frustrating to have have to look up how to rewrite git credentials each time, so here's a script to do such.
#!/bin/bash
#
# git-rewrite-author.sh
#
# Change the author credentials in a git repository. Execute with "--help"
# argument for usage information.
print_usage()
{
echo "Usage: $(basename $0) <new author> <new email>"
echo ""
echo "Uses git-filter-branch (1) to rewrite the author credentials of"
echo "a git revision history."
}
print_and_exit()
{
local message="$1"
local return="$2"
test -n "$1" && echo "$1" >&2
test -n "$2" && exit $2 || exit 0
}
rewrite_history()
{
local author="$1"
local email="$2"
git filter-branch -f --commit-filter '
GIT_COMMITTER_NAME="'"$author"'";
GIT_AUTHOR_NAME="'"$author"'";
GIT_COMMITTER_EMAIL="'"$email"'";
GIT_AUTHOR_EMAIL="'"$email"'";
git commit-tree "$@";
' HEAD
}
main()
{
# Parse --help argument
test "x$1" == "x--help" && print_and_exit "$(print_usage)"
# Fail if not enough arguments
test -n "$2" || print_and_exit "$(print_usage)" 2
# Test for git repository
test -d .git || print_and_exit "fatal: not a git repository!" 2
rewrite_history "$1" "$2"
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment