Skip to main content
Chapter 35 of 61 · 2 sections · Page 138

Git: Rewriting History with filter-branch

Bulk rewrite commit author names, emails, and dates across repository history.

Original chapter reference: Chapter 35: Rewriting history with filter-branch. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Section 35.1: Changing the author of commits

You can use an environment filter to change the author of commits. Just modify and export $GIT_AUTHOR_NAME in the script to change who authored the commit.

Create a file filter.sh with contents like so:

Source example · PDF page 138
if [ "$GIT_AUTHOR_NAME" = "Author to Change From" ]
then
    export GIT_AUTHOR_NAME="Author to Change To"
    export GIT_AUTHOR_EMAIL="email.to.change.to@example.com"
fi

Then run filter-branch from the command line:

Source example · PDF page 138
chmod +x ./filter.sh
git filter-branch --env-filter ./filter.sh

Section 35.2: Setting git committer equal to commit author

This command, given a commit range commit1..commit2, rewrites history so that git commit author becomes also git committer:

Source example · PDF page 138
git filter-branch -f --commit-filter \
   'export GIT_COMMITTER_NAME=\"$GIT_AUTHOR_NAME\";
    export GIT_COMMITTER_EMAIL=\"$GIT_AUTHOR_EMAIL\";
    export GIT_COMMITTER_DATE=\"$GIT_AUTHOR_DATE\";
    git commit-tree $@' \
    -- commit1..commit2
Buy me a coffee
Daily Practice
0/0