The highest voted answer here is now out of date. Git shows this scary warning when using git filter-branch -
WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
(https://github.com/newren/git-filter-repo/) instead.
filter-repo is not (yet) part of git and needs to be installed separately.
# Requires git v2.22+ and python v3.5+. Check with -
git --version && python3 --version
# Install using pip
pip3 install git-filter-repo
To replace only the email in previous commits run the the command like this -
git filter-repo --email-callback '
return email if email != b"incorrect@email" else b"correct@email"
'
To replace both, email and author name in the previous commits run the the command like this -
git filter-repo --commit-callback '
if commit.author_email == b"incorrect@email":
commit.author_email = b"correct@email"
commit.author_name = b"Correct Name"
commit.committer_email = b"correct@email"
commit.committer_name = b"Correct Name"
'
Make sure the indents are there when you paste the command in your terminal. The callback uses python syntax so indents are important.
Read more about filter-repo callbacks in the docs.
remote: error: GH007: Your push would publish a private email address.
... ` ! [remote rejected] master -> master (push declined due to email privacy restrictions)`. – Autocrat