r/git • u/TachikawaKan5241 • 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
View all comments
Show parent comment
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 bisecttool 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.