Technical

How I Fixed My Git Commit History Without Losing My Mind

5 min read
·
August 17, 2025
·
598 views

A Perfectionist’s Guide to Keeping Git History Clean (Without Losing Sleep).

Featured Image

We've all been there. You're working on your project, happily committing your changes one after another, and then suddenly you realize:

"Wait… why does my commit history looks like spaghetti?"

If you are like me, you probably have a strong itch to keep your Git history clean and beautiful. One little messy commit in between and it feels like a speck of dust on your freshly cleaned desk. The perfectionist in you (hi, that's me 🙋‍♂️) just cannot let it slide.

That is exactly what happened to me, and I want to share how I fixed it step by step in plain, human language, so you don't have to pull your hair out like I almost did.

The Problem

I had a series of commits, let's say 10 of them. One of the older commits was about adding a feature. But then, much later, I realized I had a small change that really belonged with that feature commit.

Here's the catch: if you just squash a new commit into an older one, Git updates the date of that commit to today's date. Suddenly, your commit history looks like you time-traveled, did all your work today, and forgot the last week even happened.

Not cool.

I wanted two things:

  1. Squash a newer commit into an older one.
  2. Preserve the original commit dates, so the timeline stays intact.

Sounds simple? Ha. At first, I wondered if Git had a "back to the past" button. (Spoiler: it does not, but we can fake it.)

🪜 The Solution Step by Step

Here is exactly what worked for me.

🛟 Step 1: Make a Backup Branch (or two 😉)

First rule of Git surgery: never operate without a backup. I created a branch from the current state:

git checkout -b backup-branch

And just to be extra safe, I made another one:

git checkout -b backup-branch-2

Because hey, perfectionists love backups. This way, even if I messed everything up, I could always go back to safety. Think of it like keeping your pizza in the oven while you experiment with a slice. 🍕

On my backup branch, I also created the new commit with the changes I wanted to squash. That way, I could test and experiment without risking the main branch.

🍒 Step 2: Cherry-Pick the Commit to Squash

Now that I had my safety net, I switched back to the main branch:

git checkout main

Then I cherry-picked the newer commit (from the backup branch) into my main branch:

git cherry-pick <sha-of-newer-commit>

Now the new commit was sitting in the main branch, right where I wanted it, ready for cleanup.

🌀 Step 3: Interactive Rebase

Here comes the magic. Instead of a normal rebase, I ran this command:

git rebase -i --committer-date-is-author-date <sha-before-older-commit>

This little flag --committer-date-is-author-date is what makes sure your commit dates stay intact. Without it, Git would happily overwrite them with today's date.

In the rebase editor, I reordered the commits, so the new commit came right after the older one. Then I marked it as fixup instead of squash.

👉 Why fixup instead of squash?

Because squash lets you edit commit messages, while fixup quietly merges the change into the previous commit without touching the original message. Since I wanted to preserve both the message and the date, fixup was perfect.

🧩 Step 4: Finish the Rebase

Once I saved and closed the rebase editor, Git neatly merged my newer commit into the older one. The best part? Thanks to that flag, the commit kept its original date.

No unwanted time travel. ✅

🚀 Step 5: Force Push

Finally, since I had rewritten history, I had to force push:

git push origin main --force

And that was it.

🎉 The Happy Ending

After doing this, my history looked exactly how I wanted. Commits were in order, the unwanted commit was merged neatly into the right one, and the dates were preserved as if nothing ever happened.

For a perfectionist like me, it was pure bliss. You know that feeling when you finally arrange your bookshelf in the exact order you wanted and then just stare at it for five minutes? That was me with my git log. And yes, I did stare at git log with a proud smile.

💡 Why This Matters

Some people might say, "Why bother, it is just commit dates." But if you care about presenting your work, a clean commit history is like writing a story with proper chapters. Future you (and anyone reading the repo) will thank you for the effort.

✅ In short: backup, rebase with fixup, preserve dates, force push.

Then sit back and enjoy the satisfying view of your perfectly aligned Git history.

Final Thoughts

Git can feel intimidating, especially when you're rewriting history. But once you understand the steps, it is like carefully editing a story, so it flows the way it was meant to.

And hey, if you mess up, that is why you made a backup branch. 😉

So next time you notice a commit out of place, don't panic. Take a deep breath, follow these steps, and you'll have a commit history that even your future self will thank you for.

Technical

Enjoyed this post?

Follow me on LinkedIn for more insights on technology, career growth, and software development.

Follow on LinkedIn