Hello AnakInformatika! Have you ever experienced a situation where you realized a commit in your Git repository had the wrong author? Whether it was a typo in the name, using a personal email for a work project (or vice versa), or simply forgetting to configure user.name and user.email during your initial Git setup, leaving your early commits looking strange?
Don't panic! This is a common issue faced by developers. Fortunately, Git provides a solution. In this comprehensive guide, we will learn How to Clean Up Git History: Changing the Author Name/Email Across Multiple Commits Using Git Rebase. Yes, you heard that right—we can clean up that messy history!
Why Do You Need to Clean Up Your Git History? (And the Diary Analogy)
Git history is like a diary for your project. Every commit is an entry recording who wrote it (author), when it happened (timestamp), and what changed (commit message and code modifications). Imagine reading a diary where several entries are written under someone else’s name, or your own name with spelling errors—it would get confusing trying to trace who actually made those changes.
In software development, keeping a clean and accurate Git history is crucial for:
-
Accountability: Makes it easy to track who is responsible for a change, especially when hunting down bugs or understanding the logic behind a feature.
-
Professionalism: Shows neatness and attention to detail in your work.
-
Collaboration: Makes it easier for team members to collaborate and understand everyone's contributions.
Git rebase acts as a "magic eraser" that allows us to edit or reorganize "entries" in our project's diary. With interactive rebase, we can step back through existing commits and make adjustments, including updating the author's name and email.
💡 Tip: Although powerful, cleaning up Git history must be done with caution. Keep this golden rule in mind: never rebase commits that have already been pushed to a shared remote repository if others have pulled those changes. If you absolutely must, communicate with your team first!
Prerequisites Before Taking Action
Before we begin our Git history "operation," make sure you have the following ready:
-
Git Installed: Check your version using
git --version. A relatively recent version (2.x or above) is recommended. -
Basic Git Understanding: You should be familiar with core Git concepts like
commit,branch,push, andpull. -
Text Editor: Git will open your default text editor (such as Vim, Nano, VS Code, or Sublime Text) during an interactive rebase. Make sure you are comfortable using it.
-
A Safe Local Repo: Ensure you are working on a local copy of your repository. Better yet, create a new branch for experimentation!
⚠️ Important Note: Changing the author will alter the SHA-1 hash of the affected commits, rewriting the history. If you are working on a shared repository, this will require a
git push --forceorgit push --force-with-lease. Always use caution and coordinate with your team before force-pushing to a shared branch!
Practical Steps to Change Authors with Git Rebase
Let's walk through a scenario where you have multiple commits with incorrect author details using a local simulation repository.
Step 1: Set Up the Simulation Repository
First, create a new Git repository and add a few commits with "incorrect" or different author settings.
# Create a new project directory
mkdir git-history-cleanup
cd git-history-cleanup
# Initialize the Git repository
git init
# Set an incorrect author for the initial commits
git config user.name "Anak Informatik"
git config user.email "anak.informatik@example.com"
# First commit with the wrong author
echo "Initial project setup" > README.md
git add README.md
git commit -m "feat: Initial project setup"
# Change author details again (e.g., using another name)
git config user.name "Dev Testing"
git config user.email "dev.testing@example.com"
# Second commit
echo "Added feature A" >> feature-a.txt
git add feature-a.txt
git commit -m "feat: Implement feature A"
# Change author details again
git config user.name "Guest User"
git config user.email "guest@example.com"
# Third commit
echo "Fixed bug B" >> bug-fix.txt
git add bug-fix.txt
git commit -m "fix: Resolve bug B"
# Finally, set the correct author details we want
git config user.name "Anak Informatika"
git config user.email "redaksi@anakinformatika.com"
# Fourth commit (with the correct author)
echo "Added feature C" >> feature-c.txt
git add feature-c.txt
git commit -m "feat: Implement feature C"
Now, check your Git history:
git log --pretty=format:"%h %an <%ae> %s" --reverse
Your output should look similar to this (commit hashes will vary):
e1f3a2f Anak Informatik <anak.informatik@example.com> feat: Initial project setup
8a2b1c3 Dev Testing <dev.testing@example.com> feat: Implement feature A
c7d8e9f Guest User <guest@example.com> fix: Resolve bug B
f0a1b2c Anak Informatika <redaksi@anakinformatika.com> feat: Implement feature C
Here, we want to update the author of the first three commits to Anak Informatika redaksi@anakinformatika.com.
Step 2: Identify Target Commits and the Base Commit
We need to determine where to start our rebase. Since we want to modify commits e1f3a2f, 8a2b1c3, and c7d8e9f, we start the rebase from the commit immediately before the first commit we want to edit.
Since e1f3a2f is the first commit in the repository, our base reference can be HEAD~4 (since there are 4 commits total) or --root.
# Ensure you are on the correct branch (e.g., main or master)
git checkout main
# Check the log again to verify commit order and count
git log --pretty=format:"%h %s" --reverse
Since we want to change the first 3 commits out of 4 total, HEAD~4 will cover all commits.
Step 3: Start Interactive Git Rebase
Run the interactive rebase command:
git rebase -i HEAD~4
Git will open your default text editor showing a list of commits:
pick e1f3a2f feat: Initial project setup
pick 8a2b1c3 feat: Implement feature A
pick c7d8e9f fix: Resolve bug B
pick f0a1b2c feat: Implement feature C
# Rebase 272c43e..f0a1b2c onto 272c43e (4 commands)
# ...
To edit the author for e1f3a2f, 8a2b1c3, and c7d8e9f, change the keyword pick to edit (or e) for those commits. Leave f0a1b2c as pick because its author is already correct.
Modify the file to look like this:
edit e1f3a2f feat: Initial project setup
edit 8a2b1c3 feat: Implement feature A
edit c7d8e9f fix: Resolve bug B
pick f0a1b2c feat: Implement feature C
Save and close the editor. Git will pause at the first commit marked as edit (e1f3a2f).
Step 4: Update the Author Name/Email for Each Commit
When Git pauses at the first commit, you will see a message like:
Stopped at e1f3a2f... feat: Initial project setup
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
Now, update the author details for the current commit:
git commit --amend --author="Anak Informatika <redaksi@anakinformatika.com>" --no-edit
Parameter Explanation:
--author="...": Explicitly sets the new author name and email.
--no-edit: Retains the existing commit message so you don't need to open the text editor again.
Once updated, proceed to the next commit:
git rebase --continue
Git will move forward and pause at the second commit marked edit (8a2b1c3). Repeat the process:
# Update author for the second commit
git commit --amend --author="Anak Informatika <redaksi@anakinformatika.com>" --no-edit
# Continue rebase
git rebase --continue
Git will pause at the third commit (c7d8e9f). Repeat one more time:
# Update author for the third commit
git commit --amend --author="Anak Informatika <redaksi@anakinformatika.com>" --no-edit
# Continue rebase
git rebase --continue
Because the fourth commit (f0a1b2c) remains marked as pick, Git skips pausing on it and completes the process:
Successfully rebased and updated refs/heads/main.
Step 5: Verify the Results
With the rebase complete, verify that all commits now feature the correct author details:
git log --pretty=format:"%h %an <%ae> %s" --reverse
Your output should now look like this:
a1b2c3d Anak Informatika <redaksi@anakinformatika.com> feat: Initial project setup
e4f5g6h Anak Informatika <redaksi@anakinformatika.com> feat: Implement feature A
i7j8k9l Anak Informatika <redaksi@anakinformatika.com> fix: Resolve bug B
m0n1o2p Anak Informatika <redaksi@anakinformatika.com> feat: Implement feature C
Congratulations! All commits now feature uniform, clean, and accurate author information. Notice that the SHA-1 hashes have changed (e.g., from e1f3a2f to a1b2c3d), confirming Git rewritten the history cleanly.
Alternative: Changing Tens or Hundreds of Commits at Once
The interactive rebase method works great for updating a few specific commits. However, if you need to fix dozens or hundreds of commits, running git rebase --continue repeatedly can be time-consuming.
For bulk updates, use the git-filter-repo utility (the official replacement for the deprecated git filter-branch).
-
Install
git-filter-repo(requires Python):Bashpip install git-filter-repo -
Create a mailmap script: Create a text file named
mailmap.txtwith the following structure:PlaintextAnak Informatika <redaksi@anakinformatika.com> Dev Testing <dev.testing@example.com> Anak Informatika <redaksi@anakinformatika.com> Guest User <guest@example.com> -
Execute the cleanup:
Bashgit filter-repo --mailmap mailmap.txtThis scans your entire history and replaces old author details with the new ones in seconds!
What If Commits Are Already Pushed to Remote?
If you clean up local history that has already been pushed to a remote repository (like GitHub or GitLab), a standard git push will be rejected due to divergent history.
To synchronize, you must force push:
git push origin main --force-with-lease
💡 Safety Tip: Always prefer
--force-with-leaseover standard--force.--force-with-leasewill abort the push if someone else on your team has pushed new commits to the remote branch, preventing accidental overwrites of their work.
Conclusion
Cleaning up your Git history is more than an aesthetic preference—it’s an investment in accountability and professionalism for your codebase. By using git rebase -i alongside git commit --amend --author, you can easily correct metadata without affecting code changes.
Remember the golden rule: be careful when rewriting history on shared branches, and always communicate with your team before force pushing.
Hope this guide helps keep your repositories clean and organized! Have questions or ran into issues during your rebase? Let us know in the comments below! See you in the next AnakInformatika article! 🚀