问题
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!
回答1:
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 "$@"'
回答2:
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.
来源:https://stackoverflow.com/questions/13552143/change-email-address-in-git-history