Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change all my commits to another on Github?

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 :)

like image 636
cbll Avatar asked Oct 28 '25 19:10

cbll


2 Answers

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: Multiple Emails on one account

This will not change the author of the commit, but will in GitHubs interface make you appear as the author.

like image 56
RandomSort Avatar answered Oct 30 '25 08:10

RandomSort


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:

Option 1: delete the WRONG_ACCOUNT

If it’s unnecessary to use the WRONG_ACCOUNT, you can delete the account.

In the WRONG_ACCOUNT settings page -> Account -> Delete your account.

enter image description here

Then you can add the WRONG_EMAIL as a secondary email address for the RIGHT_ACCOUNT.

Option 2: rewrite commit history

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.

like image 30
Marina Liu Avatar answered Oct 30 '25 08:10

Marina Liu