Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change commit author for multiple commits using filter branch?

Tags:

git

I am using this script found at this link to edit author info across all commits.

#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"

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

However, I am getting the following error(warning?):

Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f

I checked the log too. The author info didn't change. What am I doing wrong here?

Update: As mentioned by @elpiekay, the -f flag made the script work.

But can anyone explain the error log itself? Why does it mention about backup ? I never made any backup before (unsure what backup is being referred in the error log)

like image 985
Saurabh P Bhandari Avatar asked Oct 07 '19 03:10

Saurabh P Bhandari


People also ask

How do I change the author and committer name and email of multiple commits in git?

The git commit --amend --reset-author --no-edit command is especially useful if you created commits with the wrong author information, then set the correct author after-the-fact via git config .

How do I amend multiple commits?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N .


1 Answers

Actually, a better practice is to:

  • perform a filter on a freshly cloned repository
  • not used git filter-branch anymore, which is declared (as of Git 2.24, Q4 2019) as somewhat deprecated.

You can instead used right now its possible successor: newren/git-filter-repo (in Python), and its example section:

cd repo
git filter-repo --mailmap my-mailmap

with my-mailmap:

Correct Name <[email protected]> <[email protected]>

That would replace the author name and email of any commit done by anyone with <[email protected]>

See git mailmap for the exact syntax of that mapping file.

like image 145
VonC Avatar answered Nov 15 '22 09:11

VonC