Rename multiple names and emails in a single pass of git-filter-branch

Is it possible to mass-rename 2 or more email addresses with a single pass of git filter-branch?

I tried adapting the code from this answer by just duplicating the if..fi clause:

  • Puppet Modules - Best Practices for Installing External Modules and Downloading Packages
  • git merge and different gitignore for each branch
  • Forking on Google Code (or any other SVN hosting)
  • How can I keep a git fork updated?
  • How can I check whether a git repository has any commits in it?
  • git hooks - regenerate a file and add it to each commit?
  • git filter-branch --commit-filter '
            if [ "$GIT_COMMITTER_NAME" = "<Old Name 1>" ];
            then
                    GIT_COMMITTER_NAME="<New Name 1>";
                    GIT_AUTHOR_NAME="<New Name 1>";
                    GIT_COMMITTER_EMAIL="<New Email 1>";
                    GIT_AUTHOR_EMAIL="<New Email 1>";
                    git commit-tree "$@";
            else
                    git commit-tree "$@";
            fi
            if [ "$GIT_COMMITTER_NAME" = "<Old Name 2>" ];
            then
                    GIT_COMMITTER_NAME="<New Name 2>";
                    GIT_AUTHOR_NAME="<New Name 2>";
                    GIT_COMMITTER_EMAIL="<New Email 2>";
                    GIT_AUTHOR_EMAIL="<New Email 2>";
                    git commit-tree "$@";
            else
                    git commit-tree "$@";
            fi' HEAD
    

    But it gave me errors during the filter-branch that looked like this:

    error: duplicate parent bc8f9924c33558a275b8f694969529cf56232c80 ignored
    

    And then the branch history was all tangled up:

    enter image description here

  • How do I store the changes to a git repository in another server-side repository?
  • Pull all commits from a branch, push specified commits to another
  • Can't push to heroku after accidentally deleting app on heroku
  • Git client on the iPhone, possible? How?
  • Cannot remove remote origin
  • git - commit modified files from one branch to another branch
  • One Solution collect form web for “Rename multiple names and emails in a single pass of git-filter-branch”

    You want just one git commit-tree command, after setting all the environment variables as desired. For instance, your commit filter might read something like this:

    git filter-branch -f --commit-filter '
        if [ "$GIT_COMMITTER_NAME" = "<Old Name 1>" ]; then
            GIT_COMMITTER_NAME="<New Name 1>";
            GIT_AUTHOR_NAME="<New Name 1>";
            GIT_COMMITTER_EMAIL="<New Email 1>";
            GIT_AUTHOR_EMAIL="<New Email 1>";
        fi;
        if [ "$GIT_COMMITTER_NAME" = "<Old Name 2>" ]; then
            GIT_COMMITTER_NAME="<New Name 2>";
            GIT_AUTHOR_NAME="<New Name 2>";
            GIT_COMMITTER_EMAIL="<New Email 2>";
            GIT_AUTHOR_EMAIL="<New Email 2>";
        fi;
        git commit-tree "$@"
        '
    

    (although if the number of name changes to make gets higher I’d probably run through a mapping file instead of a long series of easy-to-typo if ... thens, and it might make more sense to map author and committer separately).

    Remember that filter-branch simply copies all the commits you tell it to copy (by using git commit-tree to make the new copy), while building a map of <old-sha1,new-sha1> pairs. This is why making two commits is not good: one existing (old) SHA-1 now has to map to two new copies, which is simply not allowed in terms of what filter-branch does with the commit graph. (Multiple old SHA-1s can map to a single new SHA-1, if you choose to omit some commits during the copying process. That is, the new graph can be bijective or surjective, but filter-branch doesn’t really believe in injective, as it’s going to try to map the old references to the new graph.)

    Git Baby is a git and github fan, let's start git clone.