Following on from a previous post about rewriting indentation for an entire git repo, another reason to rewrite the history of a whole repo is to change the email address or name mentioned in commits.

Once again, be aware that rewriting history will cause problems if other people have clones of the repo so make sure you understand the risks before doing this!

To change the author name and email address for every commit, you can run this:

1
2
3
4
5
6
git filter-branch -f --env-filter "
  GIT_AUTHOR_NAME='YOUR_NAME'
  GIT_AUTHOR_EMAIL='YOUR_EMAIL'
  GIT_COMMITTER_NAME='YOUR_NAME'
  GIT_COMMITTER_EMAIL='YOUR_EMAIL'
" HEAD

This will blindly replace all the commits with the details given, but if other people have contributed, you may need to be a bit more careful and only replace your own:

1
2
3
4
5
6
7
8
9
10
11
12
git filter-branch --commit-filter "
  if [ "$GIT_COMMITTER_NAME" = 'YOUR_ORIGINAL_NAME'];
  then
    GIT_AUTHOR_NAME='YOUR_NEW_NAME'
    GIT_AUTHOR_EMAIL='YOUR_NEW_EMAIL'
    GIT_COMMITTER_NAME='YOUR_NEW_NAME'
    GIT_COMMITTER_EMAIL='YOUR_NEW_EMAIL'
    git commit-tree "[email protected]";
  else
    git commit-tree "[email protected]";
  fi
" HEAD

If you understand the risks of rewriting history for a shared repository, you can force push with the -f flag:

1
git push -f

Then, to ensure future commits don’t use the wrong name and email, you can set it explicity for this particular repo (instead of globally) with these commands:

1
2
git config user.email "[email protected]"
git config user.email "[email protected]"

Keep in mind this will only work for this particular clone as the details will be stored locally in .git/config.

Search and replace, vim and git

Search and replace, vim and git Continue reading

Using netrw instead of NERDTree for Vim

Published on December 28, 2016