[原創] How to revise author name and email in commit history

hustnzj發表於2022-08-31

I pushed my repo onto bitbucket.org but found the author Email not matched to an Atlassian account.

So I need to revise the author email in my repo.

Generally, there are three ways to revise the author information (author name & email) in commit message.

git rebase

Firstly, change the user config in the local repo unless you really want to change it globally:


$ git config user.name <new name>

$ git config user.email <new email>

If you just want to edit the last commit


git commit --amend --no-edit --reset-author

If you want to edit specified commit

Using git log to find the hash of the parent commit of the one which you want to edit, i.e. if you want to edit the author information in 260dca8 commit, the parent’s hash is e1899f5.


...

9c8dbcd Add 3rd news item

260dca8 Add bio page for Mary

e1899f5 Add green page

...

Using git rebase as below:


git rebase -i e1899f5

The configured text-editor will be launched:


pick 260dca8 Add bio page for Mary

pick 9c8dbcd Add 3rd news item

Change pick before 260dca8 into edit, then save and close the editor.

Run git commit --amend --no-edit --reset-author, then run git rebase --abort.

When there are a bunch of commits pending to be edited.


git rebase -r <some commit before all of your bad commits>

--exec 'git commit --amend --no-edit --reset-author'

Note: This method is not perfect as the git filter-repo. With the -r option, any resolved merge conflicts or manual amendments in these merge commits will have to be resolved/re-applied manually.

git filter-branch

This method is very dangerous and deprecated officially.

git filter-repo

If there are a lot of commits need to be edited, git rebase is too tedious.

git filter-repo is a perfect tool to do this.

After install it and create the .mailmap file which abide by the syntax, run the command:


git filter-repo --mailmap .mailmap

Note:

It is strongly recommended to try filter-repo tool on a fresh clone. Also remotes are removed once the operation is done. Read more on why remotes are removed here. Also read the limitations of this tool under INTERNALS section.

References

  1. git - How do I change the author and committer name/email for multiple commits?

  2. newren/git-filter-repo: Quickly rewrite git repository history (filter-branch replacement)

  3. git-filter-repo/INSTALL.md at main · newren/git-filter-repo

  4. git-filter-repo(1)

  5. gitmailmap(5)

本作品採用《CC 協議》,轉載必須註明作者和本文連結
日拱一卒

相關文章