Search code examples
gitgithubgit-rebaseauthor

Git rebasing to add an author and email address to many commits


I have been involved in a group project for the period of a month.

I thought my commits were submitted in my name using my GitHub related email address and I was getting indications on my main GitHub feed that contributions were being made. However, when I have gone to inspect the project there are no commits listed under my name, it turns out that all the commits were made under my computer user name "Author: UserName <[email protected]> ". Unfortunately, as this Author is not associated with my Github account the project does not publicly recognize these commits in the contribution stats.

I have been trying to go through all the commits using git rebase -i --root assigning each of my commits for edit and then applying git commit --amend --author "MyName <[email protected]>" but I keep encountering issues with the other commits from my peers which I pick not edit during the interactive rebase.

Am I approaching this wrong or is there a simpler way to assign my authorship to these commits?

Thank you for taking the time to read this, any ideas would be greatly appreciated.


Solution

  • You'll first need to update your Git author info by updating your configs:

    https://docs.github.com/en/github/setting-up-and-managing-your-github-user-account/setting-your-commit-email-address

    While changing your Git configs will resolve all future commits, the only way to have past commits attributed to your account would be to rewrite your repository history. This requires changing your email addresses globally. Git's own documentation covers this:

    https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#_changing_email_addresses_globally

    git filter-branch --commit-filter '
            if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ];
            then
                    GIT_AUTHOR_NAME="MyName";
                    GIT_AUTHOR_EMAIL="[email protected]";
                    git commit-tree "$@";
            else
                    git commit-tree "$@";
            fi' HEAD
    

    Force push the corrected history to your remote:

     git push --force --tags origin 'refs/heads/*'
    

    IMPORTANT: Force push can bring mayhem to your repository if performed incorrectly, and is considered bad practice on repos where you aren't the owner and sole collaborator.