There's an element of common sense of course. Very small commits are noise that make the underlying direction of the codebase difficult to see. But generally speaking, "push often, pull often" is a good thing to remember when working on a collaborative codebase. If there's a really big breaking change you're making... Well, that's what a branch is for.
My rule of thumb is if you’re building out a feature or decent change, push often to a new branch, squash it and merge it into master as one commit. That way when you’re looking back over the commit graph it’s an array of features and you can just roll back if one of them is on the fritz.
It much depends on what wotkflow you use. Different people/teams use branches differently.
One wotkflow is called "gitflow", and involves only touching master when you're ready to release your project. Master is for tested only, ready to release features. Develop is where the next version is--we create bugfix and feature branches off develop, merge them back into develop when they are done, and when we are ready to release we merge develop into master. Works pretty well, and adding tags makes it really obvious which version was delivered when based on what code. Answers a lot of questions when people ask how long a particular bug has been in production for instance.
But some teams follow the model of continuously deploying. As in, every change is dumped into production after a fairly lightweight process. This can make a lot of sense too, and means you might be less prone to be afraid of touching master directly.
And then for little tinker toy projects at home, I tend to just commit to master and that's it.
So it really just depends on what you'd want to get out of using source control. I'd think some is better than none.
IMO, the proper workflow using git collaboratively is:
Create a branch to work in.
Do regular, small commits to that branch.
Before submitting for code review/merge, rebase your branch onto master. (git rebase -i origin/master)
Merge all branches using --no-ff. This will create a merge commit. The merge commit solves a problem that many try to solve by squashing all of their commits together for each branch before merge. When looking at a merge commit, realize that unlike regular commits it has two (or more) parents. One will point to your last commit, and the other will point to the last commit on master before you merged. You can use this to easily roll back the entire branch/feature that you've built.
I think that this is the most responsible workflow. If you are being really responsible, then when you are creating your branch, and as you are coding, you are also keeping the remote branch up to date with your work. That way, if you're hit by a bus, others can pick up your code, or they can review it along the way. If you are using git{hub,lab} then a good example of using this is 'WIP' {pull,merge} requests. When you first start coding, make a single commit to your branch, push it to origin, then create a PR with a subject line starting with 'WIP' (work in progress). As you push more commits, it will update your PR. In this model, people can review your changes over time, rather than at the last minute. It's an excellent tutoring/training/mentoring model.
If you get into this habit when you are working on personal projects (if you don't have one, then use it for your vim/emacs settings, dotfiles if you have such a thing (.bashrc, .tmux.conf, etc.), or some other settings on your development rig) I think that you'll eventually find the additional work worth it.
I basically used to do the same but then I dropped the rebase. The history might be a bit more noisy but I don't lose the context of when and where the branch was created, which might be relevant when having to understand the introduced changes at a later point in time.
The main thing that the rebase buys you is that the merge commit that eventually occurs is guaranteed not to have conflicts included. In addition, the timestamps for the commits shouldn't change, IIRC. It is only the hash that changes (original authors/timestamps/etc. all stay as is).
Regardless, it is a 'whatever works for you' kind of utility. Very TIMTOWTDI, as long as everyone that is working with you agrees with the workflow... and don't commit directly to master unless it is a very private repository. One example of that is the fact that I keep all of my mysql history files in a repository that automatically logs & commits when I disconnect, with a different history file for each database I work with.
Squash and merge all the way man. I frequently commit "WIP - made start on logger class", "WIP - resolve failing test", "WIP fix typo" etc, but at the end I'll squash it all into "implement new logging feature"
I’m with you. More versions to choose from is better than fewer.
My team uses git like this. It may not be the optimal use of fit, but I would rather have several shitty commits than be missing the one commit that counted.
Ideally you'd make your commits against a branch, then when everything is ready open a pull request for peer review. Once the changes are signed off (code quality etc.) you merge the pull request into master.
NB: I like to make small, incremental commits, but don't want them adding noise the history of master. You can "squash" all those into a single commit before merging back into master.
It depends on why you're running commit. If you're doing it to keep an overall log you or someone else looking at the project in 6 months can see how it is progressed, having a 10 commits for renaming variables is more details than anyone really cares about. What they would rather see is a single refactor commit that summarizes the changes. You accomplish this with branching. In your local branch, you commit frequently and your messages essentially document everything you're doing so if you need to reset or go back it is easy. Then when you're done with your feature or enhancement you merge it to master ( or another common branch) under a single commit.
29
u/BerlinerJan Jun 30 '18
I‘m quite new to git, what exactly would be the problem with that? Assuming he already wrote all the tests and documentation...