r/git • • 1d ago

How often should I commit my changes?

I am fairly new to programming and git and I was just wondering how often I should commit my changes both locally and to Github? I feel like I'm doing it to often

9 Upvotes

79 comments sorted by

View all comments

Show parent comment

-1

u/kpg_on_ao3 19h ago

What? You should commit only when you've completed a discrete, logical task. You shouldn't be polluting commit history with broken code.

2

u/symbiatch 18h ago

Why not?

Are you confusing commits with pushing to others? I can make commit after each character if I want. Yet what is pushed will be very different. And what is merged is again different.

These are entirely different concepts. Don’t confuse them.

1

u/BarneyLaurance 8h ago

I don't stick to it fully but I think only committing when you have things in working condition is a good idea. It's part of the "atomic commits" practice. It doesn't mean it has to be a big change each time - you might just rename a variable to make it clearer and commit, or a write a short function.

But if you make sure that as far as you know the code keeps working when you commit it naturally groups things together so you or anyone else looking at the history understands which changes logically go together - e.g. if its rename a variable the commit touches all the places that use that variable.

And one of the things it enables is using the git bisect tool later in case you find you did accidentally break something that you didn't have a test for. You can add the test and then let the tool find which was the first commit where the test fails to see when you broke it.

You're doing a meaningful search since you know you didn't intentionally break anything on any commit and you're looking for the one where you made a mistake. Likely all the commits before that are good (in one particular way) and all the ones after until you fix the issue will be bad.