12

I have been working on a git repository for a while and made some commits. I have been using documentation blocks in my php files, including my private email address, like this:

/**
 * Bla bla bla.
 * 
 * @author:      Nic <foo@bar.com>
 */

Now I have created a new email address and updated the documentation blocks, replacing the old address with the new one. But this change only applies to commits after the one where I changed the documentation.

How can I completely remove the old email address from git's history and replace all instances with the new address?

I have tried using git filter-branch using this blog post but without success. I get the following result:

git filter-branch --tree-filter 'git ls-files -z "*.php" |xargs -0 perl -p -i -e "s#old-email@example.com#new-email@foobar.com#g"' -- --all
Usage: git core\git-filter-branch [--env-filter <command>] [--tree-filter <command>]
        [--index-filter <command>] [--parent-filter <command>]
        [--msg-filter <command>] [--commit-filter <command>]
        [--tag-name-filter <command>] [--subdirectory-filter <directory>]
        [--original <namespace>] [-d <directory>] [-f | --force]
        [<rev-list options>...]

Could it be that the special characters of the email addresses (@ and .) are messing up the regular expression? Other than that I have no idea what's going wrong, any help is appreciated!

3 Answers 3

11

Another option is trying filter-branch this way:

git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_NAME" = "Nick NLD" ];
  then export GIT_AUTHOR_EMAIL=your_new_email@example.com;
  fi; git commit-tree "$@"'
3
  • 4
    That would only change the author of the commits, the question is looking for a way to change the email address in the file content.
    – qqx
    Nov 25, 2012 at 15:14
  • I already found that one but as qqx said it changes the commit's mail address, not the documentation block inside the file.
    – Nic Wortel
    Nov 25, 2012 at 15:16
  • as @KjellAndreassen said, remove the extra "=" from $GIT_AUTHOR_NAME, and this works like a charm! Feb 26, 2015 at 9:37
8

Seeing that Git started to display

WARNING: git-filter-branch has a glut of gotchas […] use an
     alternative filtering tool such as 'git filter-repo'
     (https://github.com/newren/git-filter-repo/) instead.

the equivalent command for that is:

git-filter-repo --email-callback \
    'return email.replace(b"old@dusty.tld", b"new@shiny.tld")'
4

I don't know why you get a usage message. Copy & pasting your command got filter branch to run for me. But, it didn't actually modify any files. That was indeed because of the @ characters in the email addresses, that cause perl to treat @email as a list variable and replace that with the (non-existing) contents of that. So the regular expression was looking for old-email.com.

That can be fixed by using \@ in place of the @ signs in the perl command. The . in the old email address is a special character for a regular expression, but it will match itself along with anything else. In this case the rest of the pattern is likely to be strict enough that that won't matter so you probably don't really need to escape that.

1
  • Thanks. Should I perhaps configure something for filter-branch to work? I'm kind of a git-noob and never used filter-branch before.
    – Nic Wortel
    Nov 25, 2012 at 16:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.