1

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?

Community
  • 1
  • 1
sandrina-p
  • 3,794
  • 8
  • 32
  • 60

1 Answers1

1

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.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thank you for explaining me again. I'm not a expert with git, so these commands are still a fog to me. I'll read carefully that SO question and try to fix this mess. – sandrina-p Jan 15 '17 at 11:40
  • If I were in this situation, I would try to get back to where you started. Perhaps you have another branch which is very similar, which you could use? Just remember to force push when you are done, and _don't_ pull in stuff from the remote; after `filter-branch` you should treat the old remote version of the branch as effectively dead. – Tim Biegeleisen Jan 15 '17 at 11:41
  • 1
    wow, what a great idea, I have another branch yes. I'll do a copy of that one and try what you said and then force merge it to master. – sandrina-p Jan 15 '17 at 11:46
  • Hmm okay, everything went nice but now each commit has [2 avatars](https://github.com/sandrina-p/s008080/commits/0.2-b). what that means? Did i miss something on the process? – sandrina-p Jan 15 '17 at 12:06
  • I'm not an expert in GitHub, but this could be because the author email and account email aren't the same. So GitHub is picking up on this by showing both avatars. Have a look [here](https://www.quora.com/How-do-I-get-my-GitHub-avatar-to-show-up-on-my-commits) for more information. – Tim Biegeleisen Jan 15 '17 at 12:08
  • Yes, i figured it out: You need to add 2 variables, GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL `git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_EMAIL" = "wrong@email.com" ]; then export GIT_COMMITTER_NAME="new name"; export GIT_COMMITTER_EMAIL=mynew@gmail.com; export GIT_AUTHOR_NAME="new name"; export GIT_AUTHOR_EMAIL=mynew@gmail.com; fi; git commit-tree "$@"'` – sandrina-p Jan 15 '17 at 12:10
  • @SandrinaPereira Feel free to update your question with that if you think it might help someone else down the road. – Tim Biegeleisen Jan 15 '17 at 12:14