DEV Community

Zaphod Dev
Zaphod Dev

Posted on

You probably don't need git filter-branch OR git-filter-repo

You want to change the author/committer for a range of commits.

Not recommended

Running git filter-branch results in a dire warning about a glut of gotchas generating mangled history

Most powerful

Downloading and using git-filter-repo takes some time, but it does the job.

export ORIGINAL_CLONE_URL=$(git remote get-url origin)

git-filter-repo --name-callback 'return name.replace(b\"Wrong Name\", b\"New Name\")' --force
git-filter-repo --email-callback 'return email.replace(b\"wrong@example.com\", b\"new@example.com\")' --partial --refs A..B --force

git remote add origin $ORIGINAL_CLONE_URL
Enter fullscreen mode Exit fullscreen mode

Easiest

By far the easiest way is using regular rebase together with --exec command to run automatically.

git rebase -i --root --exec 'git commit --amend --reset-author --no-edit'
Enter fullscreen mode Exit fullscreen mode

Notes:

  • --root means the beginning of the repo. You can replace it with HEAD~5 for the most recent 5 commits, or a range A^..B (check range with git log A^..B first)
  • --reset-author uses whatever is configured in git. You can also use:
    • --author="New Name <new@example.com>"
    • or -c user.name="New Name" and -c user.email="new@example.com"

Check your work

To make both author and committer visible:

FORMAT="    Commit: %h %d %n \
   Author: %an <%ae> %n \
Committer: %cn <%ce> %n \
  Subject: %s %n"

git log --format="$FORMAT"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)