Cleanup GIT repository author Name and Email
Fix author name and email for many commits
Maybe you have worked some time on a GIT Repository, maybe even from different computers you suddenly see you have used different Author Names and Emails.
But don’t panic!
There is a solution named git-filter-repo
.
This tool can do a lot, see the man-page, and it can help us with our problem.
Step by step
Installation
First you need to install it, on Linux with Debian Packaging System just issue
> sudo apt install git-filter-repo
On a Mac use Homebrew:
> brew install git-filter-repo
Configuration
Find all wrong commits run;
> git log|grep Author|sort -u
You need a mailmap
file which will map the wrong name to the correct name. The format for this file is a bit clunky but the pattern is like this:
Correct Name <correct-email@example.com> <wrong-email@example.com>
You can put multiple wrong-email
in separate lines and it will update all at once:
Correct Name <correct@example.com> <wrong@example.com>
Correct Name <correct@example.com> <wrong2@example.com>
The tool will look only for the second give email address as „lookup key“ and fix the author name and the email address for the commit.
Apply the changes
I have created a file named authors.txt
, verify the authors.txt
file look good then run
> git-filter-repo --mailmap authors.txt
Parsed 434 commits
New history written in 0.05 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at 48ec4e7 Ignore config for selfhosted
Rewrote the stash.
Enumerating objects: 2944, done.
Counting objects: 100% (2944/2944), done.
Delta compression using up to 4 threads
Compressing objects: 100% (813/813), done.
Writing objects: 100% (2944/2944), done.
Total 2944 (delta 2087), reused 2944 (delta 2087), pack-reused 0 (from 0)
Completely finished after 0.19 seconds.
Verify all is correct now:
> git log|grep Author|sort -u
If still some email is wrong you can repeat the procedure as often as you like.
Push the changes
You want to push the changes to the server but get now this error message:
> git push gitea
To ssh://git.example.com/me/cool-app.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'ssh://gsh://git.example.com/me/cool-app.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
This is behause the whole history is rewritten. So you need to force the push
git push gitea --force
Enumerating objects: 2907, done.
Counting objects: 100% (2907/2907), done.
Delta compression using up to 4 threads
Compressing objects: 100% (815/815), done.
Writing objects: 100% (2907/2907), 6.94 MiB | 6.55 MiB/s, done.
Total 2907 (delta 2060), reused 2891 (delta 2049), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (2060/2060), done.
remote: . Processing 1 references
remote: Processed 1 references in total
To git.example.com/me/cool-app.git
+ 4b69f27...48ec4e7 main -> main (forced update)
You can verify on Github or your other server in the commits page now the author is correct everywhere.