How to Change Author of Commit in Git History
Sometimes, I push to a Git repository from different machines, and I find that my username and email are different on each machine. This creates an inconsistency in the Git history.
How could we change the author of a single commit in Git?
Let’s figure this out.
I’m going to assume we start on the
master branch for this walkthrough.
1. Checkout the commit
First, let’s find the commit we want to replace in the logs.
git log --abbrev-commit
commit b123892 (HEAD -> master, origin/master, origin/HEAD) Author: thewrongname <email@example.com> Date: Thu Aug 27 10:10:33 2020 -0500 Commit from another computer! commit a928338 Author: simplernerd <firstname.lastname@example.org> Date: Thu Aug 27 00:28:07 2020 -0500 Commit from my personal computer! ...
Suppose we want to change the author of the first commit
b123892 (second commit author is what we want).
We start by checking out this commit.
git checkout b123892
2. Change the author
# git commit --amend --author "new_name <new_email>" git commit --amend --author "simplernerd <email@example.com>"
After running this, we’ve checked out to a new branch.
3. Checkout the branch from Step 1
# git checkout branch_name git checkout master
Warning: you are leaving 1 commit behind, not connected to any of your branches: c894824 Commit from another computer!
c894824 is our new commit hash.
4. Replace the old commit with the new one
# git replace old_hash new_hash git replace b123892 c894824
5. Rewrite future commits based on this new replacement
git filter-branch -- --all
6. Remove the replacement
# git replace -d old_hash git replace -d b123892
7. Confirm locally that your logs look correct
8. Push to branch
git push --force
More CLI Articles
- How to Install Powerline in WSL2 Terminal
- How to Zip and Unzip Files in WSL2
- How to List All Git Aliases
- How to Add, Commit, and Push in One Git Command
- How to Replace a Local Branch with a Remote Branch in Git
- How to Revert a Specific File or Folder in Git
- How to Install Hugo on WSL2
- How to Install Node.js and npm on WSL2
- How to Revert to a Previous Commit in Git without Modifying History
- How to Merge a Branch Into Another Branch in Git
- How to Fix "xcrun error invalid active developer path" with Git on macOS
- How to Update Node to Another Version on Windows WSL
- How to Use SSH with GitHub (Instead of HTTPS) on Windows WSL
- How to Delete Files with a Specific Extension in all Subdirectories Recursively
- How to Suppress Output of npm install
- How to Pass Environment Variables to Makefiles in Subdirectories
- How to Access Environment Variables in a Makefile
- How to Reset Password on Ubuntu Linux
- How to Pull a Branch from Another User's Fork using Git
- How to Align GitHub README images in Markdown
- How to Merge Development and Production Branches in Git
- How to Quickly Push to Git with a Bash Script
- WSL2: How to Fix "Virtual hard disk files must be uncompressed and unencrypted and must not be sparse"
- How to Replace a Branch with Another Branch in Git
- How to Delete a Git Branch Locally and Remotely
- How to Schedule Recurring Deploys in Netlify
- How to View All Global Packages in npm
- How to Test an npm package locally
- How to Update an npm Package (Fixing "You cannot publish over the previously published versions")
- How to Undo Changes in a Single File in Git
- How to Fix the Netlify error: "Treating warnings as errors because process.env.CI = true"
- How to Set the Default Directory for Git Bash on Windows
- How to Search Past Terminal Commands in Linux
- How to Get the Count of Files with each File Extension in Linux
- How to Find All Files with a Specific File Extension in Linux
- How to Run Multiple Commands Simultaneously in Linux
- How to Watch a File in Linux using tail
- Linux Timestamps: The Difference Between atime, mtime, ctime, and crtime
- How To Remove Duplicate Lines While Maintaining Order in Linux
- How To Count the Number of Non-Empty Output Lines in Linux
- How To Find All IP Addresses within a File in Linux with grep
- How To Rename Every File in all Subdirectories in Windows CMD