What's the Best Way to Filter Commits in a Git Repository?

February 10, 2026 4 answers
Asked by CuriousCoder23
I'm working on a deployment pipeline that requires filtering certain commits from our release branch based on their commit hashes. We currently manage this by maintaining a text file that lists folder names alongside their respective commit hashes. When we need to filter the code, a script generates a new branch, deletes everything but the index.html file and the .git directory, then checks out each folder we want based on the hashes from our text file. The downside is that this new branch loses its commit history, complicating tasks like merging if bugs are found during testing or comparing branches later on. I'm looking for better strategies to filter code we don't want to deploy to production, other than simply holding off on merges until we're ready to deploy.

Answers

TechSavvy123
It sounds like you're taking an overly complicated approach. Have you thought about using Git submodules? Keeping each folder in a separate repo could offer a cleaner solution. Also, your main branch should be deployable as is, so this method seems pretty intense for what it needs to accomplish.
Reply to this answer
CodeNinja88
Honestly, you might want to reconsider your approach altogether. Have you ever tried using feature flags? It's all about controlling what gets executed rather than worrying about which commits are in your branch. Think about managing your deployment pipeline in a way that focuses on execution rather than the code itself.
Reply to this answer
DeployMaster99
Considering your situation, if youโ€™re dealing with a monorepo, it might be worth looking into Git cherry-pick to preserve commit history. It can be a bit tricky to set up initially, but it really saves you from losing track of what changes were made.
Reply to this answer
DevFlowExpert
Your setup sounds like it could benefit from adopting a proper Git flow. Typically, youโ€™d have a master branch for production, develop for ongoing work, and a release branch for finalized features. This way, you can keep merging fixes back where they're needed without losing history. Feature flags can also help you control what gets deployed without stripping out any code.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions