Skip to content

Instantly share code, notes, and snippets.

@philipjkim
Last active March 13, 2019 01:11
Show Gist options
  • Save philipjkim/f032887427227b6ec5fcde34ab406647 to your computer and use it in GitHub Desktop.
Save philipjkim/f032887427227b6ec5fcde34ab406647 to your computer and use it in GitHub Desktop.
Bash scripts for amending author of commits at once
#!/bin/bash
# (for example, your previous author name contains `Anonymous`)
# @ PROJECT_ROOT dir: run
#
# git log | grep -B 1 "Anonymous" | grep commit | awk '{print $2}' | xargs -L 1 ./git-amend-author.sh
if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters: $#"
exit 1
fi
prev_hash=$1
new_author="John Doe <[email protected]>" # edit this variable for you
git checkout $prev_hash && git commit --amend --no-edit --author "$new_author"
ret_val=$?
if [ $ret_val -ne 0 ]; then
echo "`git commit --amend` failed"
exit 1
fi
new_hash=`git rev-parse HEAD`
git checkout master && git replace $prev_hash $new_hash
ret_val=$?
if [ $ret_val -ne 0 ]; then
echo "`git commit --amend` failed"
exit 1
fi
git filter-branch -f -- --all && git replace -d $prev_hash
echo "Successfully amended a git commit."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment