2

I've started to work on some projects in the past without giving my correct GitHub email address in the credentials of git from Terminal. So every commit was showed as follows:

enter image description here

These commits are made without my user name so these commits are also not showing in the contributions section:

enter image description here

As it is shown in image 1 that I've made a commit on March 14 but it is not shown in the contributions (because of the wrong email was set for the git credentials). Now I all my latest contributions are shown correct but I also want all the old contributions of my past projects to be shown like this. But I don't want to change the history of commits (i.e. date).

James Z
  • 12,209
  • 10
  • 24
  • 44
Aun Abbas
  • 540
  • 7
  • 18
  • 1
    Try git mailmap. See https://stackoverflow.com/questions/53629125/does-github-consider-mailmap-for-contribution-graph. And doc on the `.mailmap` feature, https://git-scm.com/docs/git-shortlog#_mapping_authors. – ElpieKay Nov 10 '20 at 09:57
  • Add your other email address to your GitHub profile. – Edward Thomson Nov 16 '20 at 10:51
  • @EdwardThomson yes I've added my correct email address for latest commits, but my question is for the previous commits that I made. Now my issue is resolved and I've posted the answer below here. Thanks – Aun Abbas Nov 16 '20 at 11:08
  • Right, I'm talking about the email address you used for the early commits. You can have multiple email addresses in GitHub, and that will associate commits made with all of them to your profile. – Edward Thomson Nov 16 '20 at 12:40

1 Answers1

2

We can use .mailmap to write all the commits but I think this is a bit difficult solution. The simpler solution that I found very useful is as follows:

--> First of all, use git shortlog -sne into your project directory to check all the users' commits with their emails.

Now, if your project has the only user that is used for commits, then you should try this:

git filter-branch -f --env-filter "
 GIT_AUTHOR_NAME='newName'
 GIT_AUTHOR_EMAIL='newEmail'
 GIT_COMMITER_NAME='newName'
 GIT_COMMITER_EMAIL='newEmail'
" HEAD

Remember: If your project has multiple contributors, it'll also update that to just one contributor that you will provide.

--> For multiple contributors, where you want to update one or specific contributor. You can try the conditional approach, e.g.

git filter-branch -f --env-filter "
if test "$GIT_AUTHOR_EMAIL" = 'currentWrongEmail'
then
    GIT_AUTHOR_EMAIL='newEmail'
fi
if test "$GIT_COMMITTER_EMAIL" = 'currentWrongEmail'
then
    GIT_COMMITTER_EMAIL='newEmail'
fi
" HEAD

At the end of both cases, use git push -f to update the remote repository instantly.

Aun Abbas
  • 540
  • 7
  • 18