Rewrite Git History 

Want to collapse all commits into ONE ? Pretend you built it all in one shot ?

git checkout main
git reset $(git commit-tree HEAD^{tree} -m "Your single commit message")
git push --force

Step 1

git checkout main  Points your current branch (main) to that new commit hash.

Switches to the main branch (or whatever branch you're cleaning up).

Step 2

git commit-tree HEAD^{tree} -m "Your single commit message"

Creates a brand new root commit (no parents) from your current working tree (all files as they are now), with your custom message.

HEAD^{tree} grabs the current tree (your latest file state).

git commit-tree creates a new commit object from that tree.

git reset <hash> moves the main pointer to the new commit (the one created with no history).

Step 3

git push --forceForce-pushes the rewritten branch to remote repository, overwriting the old history.

 

Why would you do this ?

Clean history for handoff ( If you're pushing a project for the first time , of want to hide intermediary comit messages in your branch before merging )

Hiding sensitive or messy development history.

Why NOT to do this ?

You lose the entire commit history—which means no granular tracking, no incremental changes, no understanding of how things evolved.

Force pushing is in most cases a tricky option when working with collaborators. 

This article was updated on