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

2

u/symbiatch 18h ago

People here seem to be confused of three very different things: commits, pushes, merges.

Commit as often as you like. Got one part done? Commit. Got a part half done and want to make changes but think this part would be fine? Commit and you can do whatever and come back to this if it doesn’t work out. Commit commit commit. It doesn’t need to work or even compile. It’s a local commit.

When pushing out, for a pull request for example: squash the code with rebase. Here you can simplify the commits. If it’s in the end a smaller thing maybe squash it all. Others don’t need to see how you tried three different ways and in the end used one. These commits should be helpful for review: this part was just cleanup, this part added that thing, this part added another.

And merging might be yet another squash where the whole thing is put into a single commit.

If someone is talking about “polluting the commit history” then they don’t understand these separate things or how to work. Local commits aren’t often what goes out.