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

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.