gitcommit

How do I properly change the author of a commit for the past few commits in github?


For the past few days I used a different laptop, I set up git and logged in, but I accidently used my username with a wrong email.

Today I noticed that my commits from the past few days did not show up on my github profile dashboard so checked what's going on and noticed that I used the wrong email to log in.

I tried a few different ways to fix it, but none of those worked.

1.

git rebase -i -p <The last commit with the good email>

Then for each commit.

git commit --amend --author="good name <good email>" --no-edit
git rebase --continue

Instead of just changing the author of the commits, it made new commits with my good email but didn't change the old ones.

2.

git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
     GIT_AUTHOR_EMAIL=correct@email;
     GIT_AUTHOR_NAME="Correct Name";
     GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
     GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all

as seen here, but it didn't do anything...

3. In one of the answers somebody mentioned that I should use the same command as I used.

git rebase -i -p <The last commit with the good email>

but without the "-p". I tried it, but it also made new commits and didn't keep the old commit's with the old dates.


So, I need help getting all the new commits of my history and fixing the old ones to have the good email.


Solution

  • Thank you for everybody who helped out! I ended up getting a lot of different approaches, but it didn't work out for me, I ended up having to rebase my repo multiple times to the state before the changes because things got messed up.
    In the end I contacted Github support and this is what they told me to do, and it worked. I am posting it here in hopes that it would help out somebody who has a similar issue.

    P.S. This is pretty similar to this answer suggested here but that answer didn't work when I tried it. This one has some minor changes (cloning a bare copy and performing the change there) and it did work.


    1. Before running this script, you'll need:

    1. Create a fresh, bare clone of your repository:
    git clone --bare <external repo URL>.git
    cd <reponame>
    
    1. Copy and paste the script, replacing the following variables based on the information you gathered:
    OLD_EMAIL
    CORRECT_NAME
    CORRECT_EMAIL
    
    #!/bin/sh
    
    git filter-branch --env-filter '
    
    OLD_EMAIL="your-old-email@example.com"
    CORRECT_NAME="Your Correct Name"
    CORRECT_EMAIL="your-correct-email@example.com"
    
    if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
    then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
    then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
    ' --tag-name-filter cat -- --branches --tags
    
    1. Run the script.

    2. Review the new Git history for errors.

    3. Push the corrected history:

    git push --mirror <GitHub repo URL>.git
    
    1. Clean up the temporary clone:
    cd ..
    rm -rf <reponame>.git