Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaning up debug commits in git

A lot of the time when I'm developing an app, I'll add logging statements, sometimes if it'll help debug, die() assertions, etc. Testing locally, I don't have to commit anything to git. When I want to get this over to a staging environment though with the commits in my feature branch, I'd have to commit them with a bunch of debug type code. Eventually, when I'm ready to merge the branch into a release branch, I'd love to have these numerous commits almost cleaned/compiled into one commit and it's as if the debug statements aren't there because I made one final commit to clean them up.

Is it possible to rewrite git history to merge all of these commits into one clean commit once I'm done working on my feature branch?

like image 953
randombits Avatar asked Jul 09 '26 14:07

randombits


1 Answers

As long as you haven't published your changes (git push) you can rewrite history safely with git rebase

You can git rebase -i <commit>~1 where commit is the first commit from where you want to start cleaning up and in the interactive mode, you can squash the commit to somehow merge them into a big commit. You can find good documentation about this topic here: 7.6 Git Tools - Rewriting History

Also, take a look into this, is a small hook I did to prevent debug statements to be committed, maybe you find it useful.

EDIT: I see you mention that you need to move your changes to a staging area. If this implies pushing, then rewriting the history might be problematic unless no one else pull your changes. Rewriting the history changes your commits "sha", so other members of the team can end up with duplicated commits or with many conflicts.

On the other hand, if you push somewhere, but only you are using that staging area, then you can rewrite your history locally and push again with git push -f. It will update the branch with your local changes.

Hope this helps!

like image 91
Paulo Bu Avatar answered Jul 12 '26 09:07

Paulo Bu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!