logo

Removing a file your coding agent shouldn't have committed

Pull it out of history without losing the file

Joel Clermont
Joel Clermont
2026-06-10

I still keep a relatively close eye on what my coding agent does. This includes how commits are structured and what gets included.

But, I'm not perfect, and recently I noticed (after the fact) that a scratch file for some work in progress had been bundled in with a previous commit. I wanted to remove it from that commit, but still keep the file in the working directory. What is a good way to do this?

The easiest scenario is if the unwanted file is in your most recent commit, and you haven't pushed it yet:

git rm --cached wip-notes.md # removes from git tracking, but not working directory
git commit --amend --no-edit # revises the most recent commit to no longer include the file

Ok that's the easy scenario, but what if the file was committed several commits back?

You might reach for git filter-branch here, but git itself recommends against it. Those docs call out its "plethora of pitfalls" and instead point you to a tool called git filter-repo.

It isn't bundled with git, so you need to install it separately. It's written by a core git contributor, and git's own documentation recommends it as the replacement for the older, more dangerous approach.

Once installed, removing a file from your entire history is a single command.

git filter-repo --path wip-notes.md --invert-paths

But what if you already pushed your branch with the unwanted commit to a remote server? I'll cover that in a future tip.

Here to help,

Joel

P.S. If you like small, practical fixes like this one, I've pulled a bunch of them together in a free book. Grab A Little Bit of Laravel and keep them within reach.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice.