Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change email address in Git history

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 <[email protected]>
 */

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#[email protected]#[email protected]#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!

like image 244
Nic Wortel Avatar asked Nov 25 '12 14:11

Nic Wortel


People also ask

Can you edit git history?

There are many ways to rewrite history with git. Use git commit --amend to change your latest log message. Use git commit --amend to make modifications to the most recent commit. Use git rebase to combine commits and modify history of a branch.

How do I change my email address in Git?

How to change your Git email address. While I’m in the Git username neighborhood, I’ll also add that you can view your Git email address with this command: git config user.email. And you can change your Git email address like this: git config --global user.email [your email address here] Finally, you can also see your password by viewing ...

How do I change the author of a commit in Git?

To change the author information that is used for all future commits in the current repository, you can update the git config settings so that they only apply here: cd path/to/repository git config user.name "Marty McFly" git config user.email "[email protected]" Change the author name and email globally

How do I find the user email in Git?

#1 – Use the command, git config -get [user.name | user.email] And if you enter git config user.email from the terminal from anywhere with your git initiated directory such as This approach shows all of the key configurations from your git config file, so entering the command from the terminal: will return the following:

How do I Change my Git username?

You can change your Git username like this: Another way to change it is to edit the Git config file in your HOME directory and change it there: I just did that on my test system, and it seems to work fine.


3 Answers

Another option is trying filter-branch this way:

git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_NAME" = "Nick NLD" ];
  then export [email protected];
  fi; git commit-tree "$@"'
like image 52
Michael Durrant Avatar answered Oct 03 '22 11:10

Michael Durrant


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"[email protected]", b"[email protected]")'
like image 45
Katrin Leinweber Avatar answered Oct 03 '22 13:10

Katrin Leinweber


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.

like image 41
qqx Avatar answered Oct 03 '22 12:10

qqx