Skip to main content

How to Slice Out Bad Commits and Keep Your Git Tree Clean

 

How to Slice Out Bad Commits and Keep Your Git Tree Clean


How to Slice Out Bad Commits and Keep Your Git Tree Clean

I was working on a large scale project. I messed up my git history again. It’s a tangle of messy commits I need gone.

Rewriting history manually feels like untangling headphones. It’s slow, error-prone, and I’m terrified of breaking my branch.

Here’s how to do it in simple steps

Here’s a cleaner way to slice out those unwanted commits:

I found a small but magical command: git rebase --onto to surgically remove a range of commits.

Suppose you got a git tree that has commit that looks like below:


A --- B --- C --- D --- E --- F

Now you want to delete commits B, C, D, E. You can do this easily using below:

git rebase --onto B^ E

This command rewrites history by replanting your branch’s good commits onto a new base, skipping the bad ones.


Important point to remember

If the branch you are working on is a shared branch (e.g., main), and you want to update the remote history to match your new rewritten history, you need to force-push.

This replaces the remote branch history with your local rewritten history.

  • Force-pushing rewrites history. This can break things for others if they have already pulled the previous history.
  • If you’re working on a team, coordinate with others before force-pushing.
  • Consider using --force-with-lease instead of --force as a safer option:
git push --force-with-lease

This only force-pushes if your local view of the remote is up to date and it prevents you from accidentally overwriting someone else’s push.

Final takeaway

This simple trick save me hours when I messed up my Git history. Try it for yourself and share your thoughts in comment.

Thank you. Let’s meet again with a new cool trick.

Thank you for being a part of the community

Comments

Popular posts from this blog

Exploring Google’s New Gemini CLI: The Ultimate Open-Source Dev Tool

  Google quietly released a local AI agent that builds apps, debugs code, parses your repo, and fetches real-time data, right inside your terminal. And it’s completely free. This year, the most revolutionary developer tools I’ve used didn’t come with a splashy launch or billion-dollar hype. It came as a simple CLI: Gemini CLI, a terminal-based AI agent built on top of Google’s Gemini 2.5 Pro model . At first glance, it looks like a lightweight alternative to Claude Code. But after just 10 minutes of use, it became clear: this isn’t just a convenient utility. It’s a powerful local AI development assistant that can analyze, automate, and accelerate almost every part of your software workflow. And best of all? It’s fully open-source under the Apache 2.0 license It gives you up to 1,000 free requests per day It integrates with your local filesystem, IDE, and the web And it runs entirely in your terminal , no browser needed In this guide, I’ll show you what Gemini CLI is, how it works...

React Native vs React JS — Key Difference, Advantages-Disadvantages, Limitations

  React Native vs React JS — Key Difference, Advantages-Disadvantages, Limitations React JS It is a JavaScript library that supports each face and server-side. It’s a popularly used library that focuses on developing user interfaces for mobile and internet-primarily based applications. React Native It is a cross-platform mobile framework that uses the ReactJS framework. It’s primarily used for developing native mobile applications like Windows, iOS and mechanical man. The major advantage provided by React Native is that it permits the developers to form mobile applications on varied platforms while not compromising the tip user’s expertise. Components of React JS Components of React Native Basic parts View — it is the essential building block of internet applications. Text — It helps to point out the text. The text element contains nesting, styling, and bit handling. Image — this is often a React element for showing multiple footages like network pictures and static resources. Text...

Sharpen Your Front-End Skills: Quick HTML, CSS & React Interview Challenges

  The source of this image is Chat GPT based on writing! Are you preparing for front-end developer interviews and looking for practical, hands-on ways to improve your HTML, CSS, and React skills? Whether you’re a beginner aiming to build confidence or an experienced developer brushing up on UI skills, small, targeted challenges can make a huge difference. In this article, I’ll walk you through some of the best free and low-cost resources that offer real-world front-end tasks — perfect for interview prep, portfolio building, and daily practice. 1. Frontend Mentor frontendmentor.io Frontend Mentor is one of the most popular platforms for hands-on HTML, CSS, and JavaScript challenges. You get beautifully designed templates (in Figma or image formats) and are asked to bring them to life using clean code. The platform offers difficulty levels ranging from newbie to expert, and it’s perfect for practicing responsiveness and semantic HTML. Bonus : You can even filter for React-based ...