Git Change Author and Sign

How to change author cridentials and resign all commits in a git repository

1. Change name and email using git-filter-repo

1.1 git-filter-repo installation

Ref: https://github.com/newren/git-filter-repo/blob/main/INSTALL.md

Install via pip

Although it is officially recommended to install it using package manager, my apt couldn’t find the package, so I decided to install it through pip, the not recommended method

$ pip install git-filter-repo

Add it to $PATH

Ref: https://linuxize.com/post/how-to-add-directory-to-path-in-linux/

Installing the package via pip does not place it in the $PATH, so I have to manually add the installation directory, which is ‘~/.local/bin’, to $PATH.

Append this line to .bashrc:

# add ~/.local/bin to PATH due to git-filter-repo
export PATH="$HOME/.local/bin/:$PATH"

To load the new configuration, run:

$ source ~/.bashrc

Verify installation

Check if it is installed successfully by running:

$ git filter-repo -h

1.2 Change author name and email

Ref: https://www.gloomycorner.com/changing-author-info-in-existing-git-commits/

Ref: https://stackoverflow.com/questions/58263216/how-to-change-commit-author-for-multiple-commits-using-filter-branch

Make a fresh clone of the target repo, then write a mailmap file in which the author’s name and email are specified in the following format:

Your Name <correct@email.com> <old@email.com>

For example:

john <john@noreply.email.com> <john@gmail.com>

Then save it as, say for example, my-mailmap.

To perform filter-repo, cd into the repository and run:

$ git filter-repo --mailmap ../my-mailmap

2. Sign all the commits using git filter-branch

Ref https://gist.github.com/poqdavid/2509214dd9132f6ec77685673baea4de#file-howto-md

Ref https://stackoverflow.com/questions/41882919/is-there-a-way-to-gpg-sign-all-previous-commits

Ref https://github.com/newren/git-filter-repo/discussions/209#discussioncomment-2294772

2.1 Sign all commits

$ git filter-branch -f --commit-filter 'git commit-tree -S "$@";' -- --all

2.2 Delete all backups/dupes made by filter-branch

$ git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
$ git reflog expire --expire=now --all
$ git gc --prune=now
Content Licensed under CC BY-SA 4.0. Code licensed under the MIT License.
Last updated on Oct 13, 2022 05:48 UTC