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

29

u/jorgb 1d ago

Everytime your code compiles, runs and has tests to ensure it is in working condition. Never commit broken code, only exception is in a branch for safe keeps (you can also use git stash for that) but never on your main branch.

21

u/TachikawaKan5241 1d ago

Oh right so like whenever the code is in a functioning state where if I mess something up I can just roll back to the last commit and know it will work from there?

7

u/Soggy_Writing_3912 advanced 1d ago

yes, exactly!

3

u/themule71 1d ago

More than that. It enable git bisec. Which goes several commits back, and expects working ones.

-1

u/jorgb 1d ago

Yes, if you can't trust previous versions to be working, you might as well create a zip file periodically of your codebase. It will become more and more important when your repo gets bigger and complex, then you might need to go a few versions back to debug a change

3

u/CommandLionInterface 1d ago

Disagree about the zip file. Why on earth would you need that?

3

u/jorgb 1d ago

It was meant that if you don't use git properly, why use git versioning at all, you might just go back to zipping your files as a backup. It wasn't meant seriously

1

u/BarneyLaurance 8h ago

Zipping manually is annoying but if you don't care about distinct versions and identifiable changes each with a set purpose you could use an automatic backup tool instead of git.

6

u/One-Payment434 1d ago

You're describing when to create a pull-request; standard procedure is to do development on a branch and when the code in this branch is working it is merged into main.

During development you can commit as often as you want, I haven't seen anyone yet committing too often on a development branch.

1

u/DanKegel 17h ago

I don't commit until tests are passing.

I don't create a pull requests until the branch has a clean set of commits that do something useful.

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

6

u/themule71 1d ago

Correct. But also WIP commits can be later squashed with a rebase, but of course better have a feature/wip dedicated branch at that point.

0

u/kpg_on_ao3 19h ago

why would you commit a WIP? A commit is a save point. Why would you want to revert back to broken code?

You should pick a single task/unit of work and implement it. Then commit. My current project, here's the previous few commits:

- feat: freeze Foo for hashability
  • feat(works): add chapter_ct, total_chapters to Item. Fixes #317.
  • feat(works): use Work.chapter_ct to decide when to trigger behavior X. Fixes #301.
  • fix: update models to use Work.chapter_ct
  • chore: bump DB lib to 7.1.0
  • refactor: use upsert on DTRepository
  • feat(spider): refresh index if required by chapter_ct

Each commit compiles, passes all tests, lints, etc., and they all implement one small thing completely. The messages cross-reference tickets, identify if it's a feature or a bug fix or other (chore/refactor/etc.), and often they identify which piece of the code base this commit is about.

Anyone can see from the history what a commit does, and if you look, they usually only involve a couple files. It's easy for anyone to look at and understand quickly.

Keep your commit history clean!

1

u/symbiatch 18h ago

You literally answered your own question.

“A commit is a save point.”

That doesn’t mean it’s working correctly. It’s a point. “Here I made part 1 work, everything else isn’t working” is a save point. It’s broken since it doesn’t work fully, but it’s reasonable to save it.

Why would I want to revert to that? Because part 1 works. I broke rest more. Not working, let’s go back and do differently.

And then you even list how you commit “broken code.”

You also seem to confuse this with pushes and merges which are entirely different thing.

1

u/themule71 5h ago

Yeah u/kpg_on_ao3 seems to have missed the "rebase & squash" part. That's what leaves the history "clean".

Only on demos or very simple apps the units of work are small enough to be truly independent... in real, complex cases there can be multiple moving parts (think backend, frontend, APIs), often multiple developing teams, all working on the same feature. One can complete via stubs. One implementation may need refactoring later due to performance reasons that are not evident until all pieces are put together. Yet there can be a moment when "ok, our part is done for now, let the other teams see it", that doesn't mean it's the final implementation, it may turn out to be broken, incomplete, sub-optimal, etc.

A commit, even push to a wip branch, is something I want others to see, broken or not I don't know yet.

As I wrote in another comment, you final git history should be as clean as possible to enable git bisec.

3

u/boomkablamo 1d ago

You should pretty much never be committing, or more importantly pushing, directly on main...

1

u/dmazzoni 1d ago

You can if it’s your own repo and you’re the only developer.

Even at work I keep lots of repos for my own tools and scripts. I push changes directly to main.

1

u/boomkablamo 1d ago

You can do whatever you want in your own repo. But seems like OP wants to know best practices.