Skip to main content
  1. TECHNOLOGY/

Problem solving——Local commits to GitHub repositories, contributions not shown

3 mins· ·
Yuzhen Bug Git
Yuzhen
Author
Yuzhen
Curiosity drives, Positivity thrives.
Table of Contents

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:

image.png

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

image.png

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

image.png

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!🎉

image.png

Related

Build a Personal Blog—3. Deploying with GitHub Pages
3 mins
Yuzhen Config Docs
Build a Personal Blog—2. Customizing the Blowfish Theme
3 mins
Yuzhen Config Docs
Build a Personal Blog—1. Install Hugo, Git, Configure the Blowfish Theme
3 mins
Yuzhen Config Docs
About Me
1 min
Aboutme Yuzhen
🍄Fungus found on 14 Sep 2024
1 min
Mushroom Life