In each manual and documentation about Git you can find the following recommendation: Do not commit to master, if you need to make some changes to master, you need to create a new branch first and then merge it. So, I'm interested to know why? What benefits does this approach offer? Because as far as I understand, you don't need separate branches if you want to, for example, to revert a change - you can simply do that by using previous commit's hash.
Here I found one reason - if you have many commits, it will be easier to merge branch with master then push commits one after another http://waterstreetgm.org/git-why-you-should-never-commit-directly-to-master/
But what if your workflow is divided into many small tasks and each of these tasks fits into the one commit. So, each branch contains one commit. What are the reasons not to commit to master in this case?
Those who said 'never' or 'always' (like in the accepted answer!?!) are often (always!?! 😅 ) wrong...
⚠ Like you will discover once you understand git, there are often multiple ways to do the same things and your choices will mostly be team choices.
There is absolutely no rule that prevents to commit on master
. There are advantages and disadvantages to the 2 ways of doing.
For example, the post you linked is more a problem that the writer didn't master git enough than something else...
In less than 5 minutes, he could have created its new branch and reset its local master
to the remote ref and pushed one commit on master
branch.
Here is a list that is not exhaustive...
Using feature branches (also known as GitHub flow
) :
👍 Pros:
👎 Cons:
rebase
just before the merge
could make the history easier to read)Using "trunk Based development" (and feature toggles
. Real good development practice that you should have a look) :
👍 Pros:
👎 Cons:
Choose the one that suits your team and stick to it most of the time and apply the other when you think it makes more sense and solve a new problem you encountered...
A very good, long and complete article on the subject by Martin Fowler: https://martinfowler.com/articles/branching-patterns.html (§ Comparing Feature Branching and Continuous Integration
. But you will have to read nearly all to get it well...)