How to change the commit history name (Why is my GitHub avatar missing 😱)

This article is transcoded by SimpRead 简悦, original address juejin.cn

Github commit history avatars not showing?

As the title says, the avatars in my github commit history are not showing. It’s so strange, could it be my network problem? But I’ve already enabled the fastest VPN. Upon closer inspection :eyes:, it turns out the github username was incorrect, missing a 1 in the middle.

Annoyingly, because I registered on github rather late, all the desired usernames were already taken, so I had to awkwardly add a 1 in the middle.

I wonder if anyone else has encountered this problem: github commit records having an incorrect username, causing the avatar not to display correctly. I’ve made many commits, but no contributions have appeared. I never quite figured out the final cause.

Cause

By checking the global config with git config --global -l, I found that at some point I changed it to wangxiang, which did not match my github username, resulting in an issue with all my commit records pushed to github.

As everyone knows, git configuration files can be global and per-project. If the current project doesn’t have personal information configured, the commit will use the global config info.

# Configure global-level config
git config --global user.name 'registered_name'
git config --global user.email 'registered_email'


# Configure project-level config
git config --local user.name 'registered_name'
git config --local user.email 'registered_email'


How to fix

If you’ve read my article add, commit…:eyes: does git only have these few operations? Come learn some new tricks~, you probably know that you can use amend to modify commit information (PS: if you haven’t read it, I recommend going through it; it’s packed with useful info :+1:).

Previously, using amend was only for modifying commit message and files. Here, we can use it to modify the author information of the commit. The command format is:

git commit --amend --author="{username} <{email}>" --no-edit


For example, the code I pushed to the company gitlab has a commit message showing different names than before:

At this time, you can use the above command to modify, for example change the name to 王翔 and email to wangxiang@qimingpian.com:

git commit --amend --author="王翔 <wangxiang@qimingpian.com>" --no-edit


This way, the author information of the commit record has been successfully modified.

If you’ve already updated the username and email in git config, you can also use:

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


Note: If you’ve already pushed to remote, after modification you will need to use git push -f.

But amend only supports modifying the most recent commit, so what if, like me, the names in previous commits were also incorrect? :thinking:

Using filter-branch to batch modify

What is filter-branch? According to the official documentation, it is mainly used for batch modifying git history, which is exactly what I want!

Let’s see what filter-branch can do:

  1. Remove a file from every commit;
  2. Globally modify email addresses;
  3. Make a subdirectory the new root directory.

Here I use the official provided code as an example. For my project, the commit names and emails do not match github. By checking commit records via git logs:

Then use --commit-filter for batch modification, steps are as follows:

  1. In the project root directory, create changeCommit.sh:

      # changeCommit.sh
      git filter-branch --commit-filter '
        if [ "$GIT_AUTHOR_EMAIL" = "wangxiang@qimingpian.com" ];
        then
                GIT_AUTHOR_NAME="wang1xiang";
                GIT_AUTHOR_EMAIL="756638369@qq.com";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD
    
    
    

    This script is simple; any commit whose "$GIT_AUTHOR_EMAIL" is "wangxiang@qimingpian.com" will have the GIT_AUTHOR_NAME changed to wang1xiang and GIT_AUTHOR_EMAIL changed to 756638369@qq.com.

    You can also replace based on commit name.

  2. Run this script, the process might take a bit of time

    When done, if you see the result like the above, it means success.

    If you get the message A previous backup already exists in refs/original/, it means you have run it once before. Solve it with the following command:

    git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch Rakefile' HEAD
    
    
    
    

  3. After completion, check again with git logs:

Finally, use git push -f and you’re done :tada:! Check github commit history, good, the avatars now show.

Summary

This article mainly discussed two ways to modify username and email in git commit history. This is something many people won’t encounter, so I call it a “trick”, but it can be very effective in cases like mine. If you feel it’s not useful to you right now, bookmark it anyway — maybe you’ll need it in the future.

That’s all for this article. I hope it helps you. Please feel free to like and bookmark :folded_hands:. If you find any mistakes or better solutions/suggestions, please contact me anytime.