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
2
u/Broad-Promise6954 ancient 1d ago
First, remember the fundamentals: each Git repository is independent of all other Git repositories and at some point you connect one Git repository to another and transfer commits (from the sender, to the receiver). You don't "commit to GitHub", you just commit. Later, you
git push, making your Git repository the sender and some separate and different Git repository (on GitHub perhaps) the receiver, and only then do you send some of your existing commits to them.This is, or at least can be, important because you pick which commits to send at the time you send them. This means you don't have to send the original commits you made. You can take some time to make new and improved commits, if and when that's appropriate.
What all this means is that you can commit whenever you want! It doesn't have to be working code or ready to go. Commit whenever you want to have a save point to which you can restore, like saving your progress in a game.
Obviously, achievements make good save points, so if you got something working the way you want (or think maybe you did) that's probably a good time to commit. But even failed attempts might be worth saving, if you want to try a different approach instead but reserve the option of going back to your earlier attempt again.
This is where you should consider the fundamentals again. Each commit remembers the ID of its parent commit(s), which means commits build on their parents as you go. To make a new commit that builds only on the commits that already exist in a different Git repository (say, on GitHub), keep a memory of which commits are the ones that are shared. Your own Git does this for you, automatically, via names like
origin/mainandorigin/feature-X. You can create a new local-to-your-own-personal-Git branch starting from one of these commits, and make your own private, never-shared commits on this personal branch. As long as you don't push these commits to GitHub or wherever, no one else will ever know about them.You're therefore free to replace those commits with new and improved versions, by starting a new private branch again and picking and choosing and even changing the contents of those commits as you make the new and improved ones. That's using
git cherry-pick -nor similar for instance.Once you have this practice down, it's a simple and easy step to realize that this "make new and improved commits" process is exactly what
git rebase -idoes. It just manages it without using two separate branch names. (Let's not get into the gory details of a "detached HEAD" here, which is how rebase manages the trick.)When you want to save commits you've made locally to some external storage (in case your laptop is stolen or explodes mysteriously or whatever) you can push those commits to GitHub or wherever. Until then they're only in your own personal repository.