Git
version control
root commit
amend commit
Git tips

Edit the root commit in Git?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Editing the root commit means rewriting the very first commit in a repository’s history. Git can do it, but the cost is that every descendant commit gets a new commit id because commit hashes include parent history. That makes root-commit edits safe only when you understand the impact on collaborators and remotes.

When You Actually Need This

There are only a few good reasons to edit the root commit:

  • the initial commit included the wrong files
  • you want to add something foundational such as a license
  • the first commit contains secrets or private data that must be removed

If the goal is secret removal across history, a history-rewriting tool such as git filter-repo is often more appropriate than manually amending only the first commit.

Use Interactive Rebase From the Root

The usual Git command is:

bash
git rebase -i --root

Git opens the rebase todo list starting from the first commit. Change the action for the first commit from pick to edit, save, and Git will stop at that commit.

Then make your changes and amend the commit:

bash
git add .
git commit --amend

After that, continue the rebase:

bash
git rebase --continue

This rewrites the first commit and then replays later commits on top of it.

A Small Example Flow

Suppose the root commit forgot a LICENSE file.

bash
1git rebase -i --root
2printf 'MIT License\n' > LICENSE
3git add LICENSE
4git commit --amend --no-edit
5git rebase --continue

--no-edit keeps the original commit message. Omit it if you also want to update the message.

After the rebase finishes, the branch history is different even if the visible file changes are small.

What Happens to the Remote Branch

If the branch has already been pushed, the remote still points to the old history. You must push the rewritten branch with force.

bash
git push --force-with-lease origin main

--force-with-lease is safer than plain --force because it refuses to overwrite unexpected remote changes.

Anyone else using that branch will need to realign their local history, usually by rebasing or resetting onto the rewritten remote branch.

When Not to Do This

Do not casually rewrite the root commit of a shared repository with active collaborators unless everyone knows what is happening. The further a bad commit has propagated into forks, pull requests, tags, or deployed builds, the more expensive the rewrite becomes.

If the issue is just missing documentation or a small project file, adding a new commit is often the better engineering decision. Rewriting history should solve a real problem, not satisfy aesthetic preferences.

Editing the Root Commit in a Tiny Repository

If the repository only has one commit, the root commit is also HEAD, so ordinary amend is enough:

bash
git add .
git commit --amend

No rebase is needed in that special case because there are no later commits to replay.

Common Pitfalls

  • Forgetting that changing the root commit rewrites every descendant commit id.
  • Force-pushing rewritten history without coordinating with collaborators.
  • Using root-commit editing when a normal new commit would be simpler.
  • Trying to remove leaked secrets with a partial fix instead of using a full history-rewrite tool when needed.
  • Running git push --force instead of the safer git push --force-with-lease.

Summary

  • Edit the root commit with git rebase -i --root and mark the first commit for edit.
  • Amend the stopped commit, then continue the rebase.
  • If the branch is already published, you will need a force push.
  • Rewriting the root changes every later commit hash.
  • Use this only when history really needs to change; otherwise add a new commit instead.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.