I created a project on GitHub, which other people contributed to. Now I realized I have been logged into the wrong GitHub user on my laptop where I have committed to this repository from.
There are other people in the commit history at this point as well. As the owner of the repository(from the actual user I intended to commit from), is there any way I can "transfer" my commits from one user to another retrospectively, removing everything from the "WRONG_USER", and adding it to "RIGHT_USER" without deleting the entire .git, and starting from scratch(since the other contributors commits would have to remain)?
Thanks :)
You are in a bit of a pickle.
Commits are immutable, and author information is on the commit.
This means that you can't move commits to a different author without rewriting history.
If you are the owner of the "wrong" email-address another solution would be to add that secondary email-address to your GitHub account.
This can be seen in this image:

This will not change the author of the commit, but will in GitHubs interface make you appear as the author.
You can not add WRONG_EMAIL as a secondary email for the RIGHT_ACCOUNT since you have already used WRONG_EMAIL created another github account (let’s call it WRONG_ACCOUNT here).
There are ways to change the commits from WRONG_USER to RIGHT_USER:
If it’s unnecessary to use the WRONG_ACCOUNT, you can delete the account.
In the WRONG_ACCOUNT settings page -> Account -> Delete your account.

Then you can add the WRONG_EMAIL as a secondary email address for the RIGHT_ACCOUNT.
You can use below script to change the WRONG_USER to RIGHT_USER:
git filter-branch --commit-filter '
        if [ "$GIT_AUTHOR_EMAIL" = "WRONG_EMAIL" ];
        then
                GIT_AUTHOR_NAME="RIGHT_USERNAME";
                GIT_AUTHOR_EMAIL="RIGHT_EMAIL";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD
Then you can force push the changes to your github repo by git push -f --all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With