Search code examples
gitrebase

Merge conflicts while trying to change author of the first commit with interactive rebase


I have a project with large number of commits (more than 1000) merged from different branches. I need to change author's name of the first commit.

Tried to do it with

git rebase -i --root

Set "edit" near this commit. Then used

git commit --amend --author="Author Name <email@address.com>
git rebase --continue

And I get an unimaginable amount of merge conflicts... Too much to resolve them manually.

The first error is (if resolve it I get similar errors after):

CONFLICT (add/add): Merge conflict in README.md
Could not apply a03a0ce1... master # Merge branch 'master' into 'dev'

Tried to use --rebase-merges flag but got the same result.

Does anybody know why it happens? Any other ways to solve? Thanks!


Solution

  • Seems like you have problems in old commits. Try this

    git filter-branch command. It lets rewrite history, preserve all information (including original commit times or merge information) https://git-scm.com/docs/git-filter-branch

    git filter-branch --env-filter '
    WRONG_NAME="wrong author"
    NEW_NAME="correct author"
    NEW_EMAIL="email@mail.com"
    
    if [ "$GIT_COMMITTER_NAME" = "$WRONG_NAME" ]
    then
        export GIT_COMMITTER_NAME="$NEW_NAME"
        export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
    fi
    if [ "$GIT_AUTHOR_NAME" = "$WRONG_NAME" ]
    then
        export GIT_AUTHOR_NAME="$NEW_NAME"
        export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
    fi
    ' --tag-name-filter cat -- --branches --tags
    

    (can use filter WRONG_NAME or WRONG_EMAIL here)

    or this

    Use a git-filter-repo tool https://github.com/newren/git-filter-repo

    1. Install git-filter-repo
    2. Create a fresh clone of your project.
    3. Check email of invalid author with git shortlog --email
    4. Create a file .mailmap with content NewName <new-email-address@new.com> <old-mail@mail.ru>
    5. Command git filter-repo --use-mailmap