I have a branch, and almost all commits had a wrong email "mywrong@email.com"
and I wanted to change that e-mail to my current email "mynew@email.com"
.
While searching, I found this:
git filter-branch --commit-filter 'if [ "$GIT_COMMITTER_EMAIL" = "mywrong@email.com" ];
then
export GIT_AUTHOR_NAME="Sandrina Pereira";
export GIT_AUTHOR_EMAIL=mynew@email.com;
export GIT_COMMITTER_NAME="Sandrina Pereira";
export GIT_COMMITTER_EMAIL=mynew@email.com;
fi; git commit-tree "$@"'
See here the difference between COMMITTER and AUTHOR. It is important to really change the commit auth, otherwise wit will show that mynew
did a commit under oldnew
original commit. And here I want to change both author and commiter.
Then I did git commit -am "change author"
, git pull
and git push
.
The problem is that now all my commits are duplicated as you can see here
I searched how to delete those commits and I found this:
git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "mywrong@email.com" ];
then skip_commit "$@";
else git commit-tree "$@";
fi' HEAD
But I didn't try it yet... What should i do?
And on the next time I want to replace the e-mail commits what is the right command to avoid this mess?
Your question is a duplicate of a handful of other questions, but I will give an answer so that this question has some clousure.
You used git filter-branch
from this question, in order to correct the email on a handful of commits. This succeeded, however you then took the following incorrect actions:
Then I did git commit -am "change author", git pull and git push.
I don't think you needed to make a commit, but what is problematical is the git pull
. This pulled in the alternate original version of the branch on the remote, and then merged it into your local branch. This resulted in the commits which you rewrote becoming duplicated. Here is what you should have done immediately after running filter-branch
:
git push --force origin master # assumes your branch is master; change if needed
This would overwrite the remote branch, replacing it with the version you have created locally, containing the update email addresses. Keep in mind that filter-branch
, like git rebase
, rewrites the history of a Git branch. As a result, the finishing step to bring the branch to the remote is always doing a force push, to rewrite that remote history.
See this SO question for some tips to recover from the situation you are in now, but realize that just doing a force push would have avoided this problem to begin with.