DEV Community

Suraj
Suraj

Posted on

Fix Wrong Authors Commits the Safe Way Using Git Rebase

Introduction

Ever committed code with the wrong Git author? Whether you're juggling personal and work accounts or just forgot to configure your identity in a new project, this happens to the best of us. The good news? You can fix it safely and in this guide, we'll do it the smart way using a Docker sandbox plus real GitHub practice.

What You'll Learn

  • Git rebase fundamentals (interactive mode explained simply)
  • Three real-world scenarios for fixing wrong authors
  • Safe Docker lab environment (no risk to your actual Git setup)
  • Hands-on GitHub practice (see the fixes in action)

Why This Approach Works

Docker Container: Keeps your global Git config safe while you experiment

Real GitHub Repo: See the actual before/after results in GitHub's UI

Progressive Scenarios: From simple local fixes to complex merged commits

We'll simulate this common mistake:

  • Global user (wrong)surajdeveloper <surajdeveloper@example.com> → You have made commit with this
  • Repo user (correct)surajdevops <surajdevops@example.com> → But you should have made the commit with this.

Git Rebase : The Foundation

Before diving into fixes, let's understand why we use Git rebase for author corrections.

What is Git Rebase?

Takes your commits and replays them on top of another branch, creating a straight line history.

git rebase main
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Moves your commits to the tip of main branch
  • Creates new commit IDs (rewrites history)
  • Makes history look like you worked on the latest code all along

What is Git Merge?

Git Merge = Combines two branches by creating a merge commit that joins them together.

git merge feature
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Keeps original commits unchanged
  • Creates one new "merge commit"
  • Preserves the branching structure

When to Use?

  • Merge: When working with others on the same branch
  • Rebase: When cleaning up your local commits before opening a PR

Golden Rule: Never rebase shared branches!

Why Use Interactive Rebase for Fixes?

If you only need to fix the latest commit, you can use:

git commit --amend --author="Correct Name <correct@email.com>"
Enter fullscreen mode Exit fullscreen mode

But if the wrong author is buried in the middle of history, you need to:

  1. Stop at that specific commit
  2. Fix the author information
  3. Replay the remaining commits

That's exactly what git rebase -i (interactive rebase) does!

Rebase vs Merge: Quick Comparison

  • Merge = Combines two branches while preserving original commit history
  • Rebase = Replays commits on a new base, rewriting history for a clean linear timeline

Interactive Rebase: What Happens Under the Hood

When you run git rebase -i HEAD~3, here's the journey:

image

Visual: Before & After (Actual Git Output)

Before (wrong author on commits):

$ git log --oneline --graph --pretty=format:'%h - %an <%ae> - %s'
* 9a3f8d2 - surajdeveloper <surajdeveloper@example.com> - Add gitignore
* 7b2c4e1 - surajdeveloper <surajdeveloper@example.com> - Add main application file
* 5d1a3b9 - surajdevops <surajdevops@example.com> - Initial commit
Enter fullscreen mode Exit fullscreen mode

Notice commits 9a3f8d2 and 7b2c4e1 have the wrong author (surajdeveloper).

During interactive rebase:

$ git rebase -i HEAD~3
# Editor opens showing:
pick 5d1a3b9 Initial commit
pick 7b2c4e1 Add main application file
pick 9a3f8d2 Add gitignore

# You change it to:
pick 5d1a3b9 Initial commit
edit 7b2c4e1 Add main application file  # <- mark as edit
edit 9a3f8d2 Add gitignore              # <- mark as edit
Enter fullscreen mode Exit fullscreen mode

After fixing with rebase + amend:

$ git log --oneline --graph --pretty=format:'%h - %an <%ae> - %s'
* 2f8a9c5 - surajdevops <surajdevops@example.com> - Add gitignore
* 4e7b3d1 - surajdevops <surajdevops@example.com> - Add main application file
* 5d1a3b9 - surajdevops <surajdevops@example.com> - Initial commit
Enter fullscreen mode Exit fullscreen mode

✅ All commits now show the correct author (surajdevops)!

⚠️ Note: The commit hashes changed (9a3f8d2 → 2f8a9c5) because we rewrote history.

🐳 Setting Up Your Docker Git Lab: Let's do it hands-on

Let's create a completely isolated environment for safe experimentation.

Build & Run the Lab with the Docker image and clone the GitHub repo:

Image: https://hub.docker.com/r/surajkumar00/git-rebase-lab

GitHub repo: https://github.com/Suraj-kumar00/learn-git

Run interactive container

# Run interactive container
docker run -it --rm surajkumar00/git-rebase-lab

git clone https://github.com/Suraj-kumar00/learn-git.git
cd learn-git
Enter fullscreen mode Exit fullscreen mode

You're now inside a clean Ubuntu container and GitHub repo with Git ready to go!

Lab Exercise: Creating the Problem

Let's simulate the real-world scenario where you accidentally use the wrong Git identity.

Step 0: Create a feature branch

git checkout -b feature

# Check the branch
git branch
Enter fullscreen mode Exit fullscreen mode

Step 1: Set Wrong Global Identity

Inside the Docker container:

git config --global user.name "surajdeveloper"
git config --global user.email "surajdeveloper@example.com"

# Verify it's set
git config --global --list
# or
git config --list
Enter fullscreen mode Exit fullscreen mode

Step 2: Let's make some Commits

echo "console.log('Hello World');" > app.js
git add app.js
git commit -m "Add main javascript application file"

echo "print('Hello World')" > main.py
git add main.py
git commit -m "Add main python application file"

echo "node_modules/" > .gitignore
git add .gitignore
git commit -m "Add gitignore"
Enter fullscreen mode Exit fullscreen mode

Step 3: Check the Damage

git log

# or

git log --oneline

# or

git log --pretty=format:"%h %an <%ae> %s"
Enter fullscreen mode Exit fullscreen mode

Output shows all commits have the wrong author:

a1b2c3d surajdeveloper <surajdeveloper@example.com> Add gitignore
b4c5d6e surajdeveloper <surajdeveloper@example.com> Add main python application file
c7d8e9f surajdeveloper <surajdeveloper@example.com> Add main javascript application file
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Correct Repo-Level Identity

git config user.name "surajdevops"
git config user.email "surajdevops@example.com"

# Verify repo config overrides global
git config user.name
git config user.email
Enter fullscreen mode Exit fullscreen mode

New commits will now use the correct author, but what about the existing ones?

Scenario 1: Fix Local Commits (Before Push)

This is the safest scenario — commits exist only on your local machine.

Interactive Rebase in Action

# Start interactive rebase for last 3 commits
git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

This opens your editor with something like:

pick c7d8e9f Add main javascript application file
pick b4c5d6e Add main python application file
pick a1b2c3d Add gitignore

# Commands:
# p, pick <commit> = use commit
# e, edit <commit> = use commit, but stop for amending
# r, reword <commit> = use commit, but edit the commit message
Enter fullscreen mode Exit fullscreen mode

Mark Commits for Editing

Change pick to edit for commits you want to fix:

edit c7d8e9f Add main javascript application file
edit b4c5d6e Add main python application file
edit a1b2c3d Add gitignore
Enter fullscreen mode Exit fullscreen mode

Save and exit (in vim: esc shift+: press x)

Fix Each Commit

Git will stop at each commit marked for editing:

# When Git pauses at first commit
git commit --amend --author="surajdevops <surajdevops@example.com>" --no-edit

# Continue to next commit
git rebase --continue

# Repeat for each commit Git stops at
Enter fullscreen mode Exit fullscreen mode

Verify the Fix

git log --pretty=format:"%h %an <%ae> %s"
Enter fullscreen mode Exit fullscreen mode

Now all commits show the correct author! ✅

Scenario 1.1: Fix Initial Commit (Including Root)

Special case: What if the initial commit also has the wrong author? Regular HEAD~N won't include the root commit.

The Problem: Initial Commit Has Wrong Author

git log --pretty=format:"%h %an <%ae> %s"
Enter fullscreen mode Exit fullscreen mode

Output shows:

a1b2c3d surajdeveloper <surajdeveloper@example.com> Add gitignore
b4c5d6e surajdeveloper <surajdeveloper@example.com> Add main python application file
c7d8e9f surajdeveloper <surajdeveloper@example.com> Add main javascript application file
d8e9f0g surajdeveloper <surajdeveloper@example.com> Initial commit  ← WRONG!
Enter fullscreen mode Exit fullscreen mode

The Solution: Use -root Flag

# Include the initial commit in interactive rebase
git rebase -i --root
Enter fullscreen mode Exit fullscreen mode

This opens the editor with ALL commits including the root:

pick d8e9f0g Initial commit          ← Root commit included!
pick c7d8e9f Add main javascript application file
pick b4c5d6e Add main python application file
pick a1b2c3d Add gitignore
Enter fullscreen mode Exit fullscreen mode

Mark Commits (Including Root) for Editing

edit d8e9f0g Initial commit          ← Fix the root!
edit c7d8e9f Add main javascript application file
edit b4c5d6e Add main python application file
edit a1b2c3d Add gitignore
Enter fullscreen mode Exit fullscreen mode

Fix Each Commit (Same Process)

# Git stops at root commit first
git commit --amend --author="surajdevops <surajdevops@example.com>" --no-edit
git rebase --continue

# Continue for each subsequent commit...
Enter fullscreen mode Exit fullscreen mode

Why -root is Essential

  • Regular rebase (HEAD~4) excludes the initial commit
  • Root rebase (-root) includes everything from the beginning
  • This is the only way to edit the initial commit's author

Scenario 2: Wrong Author Already Pushed (Branch Not Merged)

If you've already pushed the commits to a feature branch (but not merged to main), you can still fix them.

Setup: Push to Remote Branch

First, let's simulate this scenario:

# Create a feature branch (if not already on one)
git checkout -b feature/user-auth

# Push to remote (if you have access to push)
git push -u origin feature/user-auth
Enter fullscreen mode Exit fullscreen mode

Fix and Force Push

After fixing the commits locally (using the same rebase process above):

# Force push the corrected history
git push origin feature/user-auth --force
Enter fullscreen mode Exit fullscreen mode

⚠️ Important Warnings:

  • Only force push if you own the branch
  • Coordinate with your team before rewriting pushed history
  • Never force push to main or master without team agreement

Scenario 3: Wrong Author Already Merged to Main

This is the most complex scenario. Once commits are merged into the main branch, rewriting history becomes dangerous.

Option 1: Accept and Document (✅ Recommended)

Best practice → Don't rewrite main branch history. Instead, add documentation:

git commit --allow-empty -m "docs: commits abc123-def456 were authored by surajdevops, not surajdeveloper"
Enter fullscreen mode Exit fullscreen mode

Option 2: Rewrite Main (⚠️ Dangerous)

Only do this if:

  • Your entire team agrees
  • You can coordinate a synchronized reset for all team members
  • The repository is not public or widely used
# Fix commits using rebase
git rebase -i HEAD~5  # Adjust number as needed

# Force push main (DANGEROUS!)
git push origin main --force

# All team members must then:
# git fetch origin
# git reset --hard origin/main
Enter fullscreen mode Exit fullscreen mode

Advanced Tips & Best Practices

For this you can read my this blog Managing Multiple GitHub/Git Accounts on One Machine (Personal + Work)

Prevent Future Mistakes

  1. Set repo-specific config immediately in new projects:
# Right after git clone or git init
git config user.name "Your Correct Name"
git config user.email "your.correct@email.com"
Enter fullscreen mode Exit fullscreen mode
  1. Create Git aliases for quick identity switching:
# Add to ~/.gitconfig
git config --global alias.work 'config user.email "work@company.com"'
git config --global alias.personal 'config user.email "personal@gmail.com"'

# Usage: git work or git personal
Enter fullscreen mode Exit fullscreen mode

When NOT to Rewrite History

Never rewrite history when:

  • ❌ Commits already merged to main/master
  • ❌ Public repositories with external contributors
  • ❌ Other team members have based work on your commits
  • ❌ CI/CD systems have already processed the commits
  • ❌ The commits have been tagged for a release

Quick Reference: Rebase Commands

# Fix last commit only
git commit --amend --author="Name <email>"

# Fix multiple recent commits
git rebase -i HEAD~N  # N = number of commits

# Fix commits including the initial commit
git rebase -i --root

# Fix commits after specific commit
git rebase -i abc123^  # abc123 = commit hash

# Abort rebase if something goes wrong
git rebase --abort

# Skip a problematic commit during rebase
git rebase --skip
Enter fullscreen mode Exit fullscreen mode

Complete Setup Commands Summary

# 1. Run the Docker lab
docker run -it --rm surajkumar00/git-rebase-lab

# 2. Clone the practice repo
git clone https://github.com/Suraj-kumar00/learn-git.git
cd learn-git

# 3. Create feature branch
git checkout -b feature

# 4. Set wrong identity and create commits
git config --global user.name "surajdeveloper"
git config --global user.email "surajdeveloper@example.com"

# 5. Make commits (will have wrong author)
echo "console.log('Hello World');" > app.js && git add app.js && git commit -m "Add main javascript application file"
echo "print('Hello World')" > main.py && git add main.py && git commit -m "Add main python application file"
echo "node_modules/" > .gitignore && git add .gitignore && git commit -m "Add gitignore"

# 6. Set correct identity
git config user.name "surajdevops"
git config user.email "surajdevops@example.com"

# 7. Fix the commits

# or git rebase -i --root for initial commit
git rebase -i HEAD~3

# Change 'pick' to 'edit', then for each commit:
git commit --amend --author="surajdevops <surajdevops@example.com>" --no-edit
git rebase --continue

# 8. Verify the fix
git log --pretty=format:"%h %an <%ae> %s"
Enter fullscreen mode Exit fullscreen mode

Your Next Steps

  1. Configure proper Git identities in all your current projects to prevent future mistakes
  2. Share this guide with your team to prevent future author mix-ups

Remember: Git is powerful, but with great power comes great responsibility hehe. Always practice in safe environments before applying these techniques to important repositories!

Happy rebasing!

Found this helpful? Star the practice repo and share with your team! Questions or improvements? Open an issue on the above GitHub repo.

Top comments (0)