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/jorgb 1d ago

That depends on how the organization has their branching setup and development flow, if pull requests are a thing, sure. OP asked when to commit, and I just mentioned "commit tested, and working code only". I used VCS and git for a long time without pull requests, and when I work solo I only bother with branches if the code is complex enough to warrant multiple commits to get to an end goal.

3

u/One-Payment434 1d ago

IME if you are only committing when you have something that is in working condition you are not committing often enough, or only doing trivial things.

1

u/AlleyOcelot 17h ago

Put "WIP" and a description of where you're at / what the holdup is to it working in the commit message. ex 'product filter WIP, missing UI classes'. Hard second to everyone saying don't go over 1/2 day without a commit. Do all this on a non merged branch.

-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.

2

u/symbiatch 18h ago

Committing still is separate from pushing. If you’re always working on main branch and also pushing every commit you make you’re in a wildly different world from most others.