Skip to main content
How to Change Git Commit Author After Commit
  1. Blog/

How to Change Git Commit Author After Commit

715 words
Git Tutorial - This article is part of a series.
Part 4: This Article

How to Change Git Commit Author After Commit
#

What to do when you find that the name in your local Git config doesn’t match your GitHub username after committing

Scenario
#

In daily development, we often encounter this scenario:

  1. You configured a temporary username (e.g., OldName) when initializing Git locally, committed and pushed code to a feature branch (e.g., fix_typo) in your fork repository, and even created a PR before realizing that this username doesn’t match your GitHub username (e.g., muzimu).

  2. The inconsistencies cause issues: Commit author names are displayed incorrectly on GitHub, cannot be properly linked to your personal contribution statistics, or do not comply with team commit guidelines.

  3. Core requirement: Batch modify the author names of historical commits without affecting PR status or needing to recreate the PR, given that the PR hasn’t been merged and multiple commits have been made.

Complete Tutorial
#

Prerequisites
#

  1. Verify current branch: Execute git branch to ensure you are on the feature branch that needs modification (e.g., fix_typo, the branch from which the PR was created).

  2. Verify remote repository: Execute git remote -v to verify that origin points to your fork repository (not the original), with output similar to:

origin  git@github.com:yourGitHubUsername/repositoryName.git (fetch)
origin  git@github.com:yourGitHubUsername/repositoryName.git (push)
  1. Record key information:

    • Old username: Execute git log and check the name after Author: (e.g., OldName).
    • New username: Your target GitHub username (e.g., muzimu).
    • Associated email: The email associated with your GitHub account.

Step 1: Batch Modify Author Names of Historical Commits
#

For scenarios with multiple commits already made, use git filter-branch to rewrite all commit author information in the current branch in batches. Copy and execute the command directly (only need to modify 3 parameters):

# Batch modify author name and email for all commits in current branch

git filter-branch --env-filter '

# 3 parameters to modify

OLD_NAME="YourOldGitName"

NEW_NAME="YourGitHubUsername"

NEW_EMAIL="YourGitHubEmail"

# No need to modify below

if [ "$GIT_COMMITTER_NAME" = "$OLD_NAME" ]; then
    export GIT_COMMITTER_NAME="$NEW_NAME"
    export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi

if [ "$GIT_AUTHOR_NAME" = "$OLD_NAME" ]; then
    export GIT_AUTHOR_NAME="$NEW_NAME"
    export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi

' -- --all
  • After execution, the terminal will display Rewrite xxx (x/x) indicating that it is rewriting commits. A final message Ref 'refs/heads/yourBranchName' was rewritten means the modification is successful.
  • If there is a cache conflict, first execute rm -rf .git/refs/original/ to clear the cache and then re-run the above command.

Step 2: Safely Force Push to Remote Branch
#

After modifying local commit history, the local and remote branch records are inconsistent, and a force push is needed to overwrite the remote branch (PR will sync automatically):

# Step 1: Refresh locally cached remote branch information (avoid stale info errors)

git fetch origin yourFeatureBranchName  # e.g., git fetch origin fix_typo

# Step 2: Safe force push (recommended to prevent accidental overwrites)

git push --force-with-lease origin yourFeatureBranchName  # e.g., git push --force-with-lease origin fix_typo

# Alternative: If the above command still errors, directly force push (only for branches you developed yourself)

# git push -f origin yourFeatureBranchName
  • After a successful push, the terminal will prompt rewriting history or Total 0 (delta 0).

Step 3: Verify Modification Results
#

  1. Local verification: Execute git log to check if all commits’ Author: fields have been updated to the new username.
  2. Remote verification: Open the GitHub PR page, refresh it, and the author names of all commits will update synchronously and can properly link to your GitHub account avatar.

Step 4: Configure Default Author for New Commits
#

To prevent new commits from using the old username again, configure Git information for the current repository or globally:

# Apply to current repository only (recommended to avoid affecting other repositories)

git config user.name "YourGitHubUsername"

git config user.email "YourGitHubEmail"

# Apply globally (all new commits in local repositories will use this configuration)

# git config --global user.name "YourGitHubUsername"

# git config --global user.email "YourGitHubEmail"

Common Pitfalls
#

1. Force push error: “stale info”
#

  • Reason: The locally cached remote branch information is inconsistent with the actual state on GitHub, and the --force-with-lease safety mechanism rejects the push.
  • Solution: First execute git fetch origin yourBranchName to refresh the cache, then re-push.

2. Commits cannot be linked to GitHub account after modification
#

  • Reason: The newly configured user.email is not associated with your GitHub account.
  • Solution: Add and verify the email in GitHub’s “Settings → Emails”.
Git Tutorial - This article is part of a series.
Part 4: This Article