Problem Description #
Recently, I’ve been using GitHub to configure and update my blog. However, today I noticed that my contributions are not being recorded on GitHub, and the commit history in the repository shows the following:
Cause #
The Git
on my Mac was installed using Homebrew
, and I didn’t configure it locally. As a result, when I pushed local files to the repository using the terminal, the default user.name
and user.email
for Git were from my local machine, not my GitHub account.
1. Check the commit history #
$git log
Even though I’m pushing to my GitHub repository, the username and email are from my local machine.
2. Check your local Git configuration #
$git config user.name
$git config user.email
If the result is blank, it means GitHub isn’t tracking the contributions because the commits aren’t linked to your GitHub account.
Solution #
1. Configure Git locally #
$git config --global user.name "github accountname"
$git config --global user.email "github@xx.com"
Here, the --global
flag applies the changes to all repositories on your machine. If omitted, the change will apply only to the current repository.
Once you configure it correctly, future commits will reflect your GitHub account and will be counted as contributions.
2. Modify previous commit history #
Update the commit history to associate previous commits with your GitHub account.
# Modify author information for past commits
$git filter-branch -f --env-filter '
if [ "$GIT_AUTHOR_NAME" = "oldName" ]
then
export GIT_AUTHOR_NAME="newName"
export GIT_AUTHOR_EMAIL="newEmail"
fi
' HEAD
# Modify committer information for past commits
$git filter-branch -f --env-filter '
if [ "$GIT_COMMITTER_NAME" = "oldName" ]
then
export GIT_COMMITTER_NAME="newName"
export GIT_COMMITTER_EMAIL="newEmail"
fi
' HEAD
NOTICE
If you write your own commit, then both are you.
Some projects have people who don’t have commit access, so you need to give them to someone who has the access to commit after modification, so you are AUTHOR and not COMMITTER!
If the modification is successful prompt:Ref ‘refs/heads/master’ was rewritten
If the modification fails to prompt: Ref ‘refs/heads/master’ is unchanged here may be because the fill in the oldName
is not found.
If you want to change it without any difference, remove the if...fi
$git filter-branch -f --env-filter "
GIT_AUTHOR_NAME='newName';
GIT_AUTHOR_EMAIL='newEmail';
GIT_COMMITTER_NAME='newName';
GIT_COMMITTER_EMAIL='newEmail'
" HEAD
3. Push changes to the remote repository #
- Force push after modifying the commit history
$git push -u origin master -f
This command forces the changes to the remote repository, overriding existing data. This should be used with caution, especially in collaborative projects.
- Pull the latest changes before pushing
$git pull origin main
$git push -u origin main
- If you want to avoid merging remote changes, create a new branch
$git branch [name]
$git push -u origin [name]
Once done, your contributions should be successfully updated!🎉